C++创建和使用类

问题描述:

我必须为作业创建一个类,我已经完成了所有我可以做的工作&我已经完成了研究并阅读了我的教科书。我还需要做些什么才能让我的课程在我的主要课程中完成?你需要知道的一切都在代码描述中。C++创建和使用类

/* LAB07.cpp 
    ALEXANDER YHAP 
    04/2012 

    In this lab you will create a new class called LabMetaData. Objects of this 
    class could be used in future lab assignments to store information about 
    the lab itself. 

    An object of class LabMetaData has the following attributes: 
    . Lab Number - A whole, positive number. Zero is valid. 
    . Lab Title - A title for the Lab Assignment 
    . Lab Author - The name of the programmer that wrote the lab. 
    . Lab Data - The date the lab was written, stored as three integer 
     numbers. The Day must be between 1 and 31. The month must be between 1 
     and 12. The year must be 4 digits and in the 21st Century (between 2000 
     and 2099). 
    . Lab Description - A description of the Lab Assignment. 

    An object of class LabMetaData has the following methods: 
    . Constructor - set the Lab Number to zero, the Lab date to 1/1/2010, 
     and all other attributes to empty strings. (Hint: call the SetData() 
     from the constructor function to avoid duplicating your code) 
    . SetData() - sets the attributes of the object to the parameters as long 
     as the parameters are valid. Rules: 
     o ALL of the parameters must be valid in order for ANY of the 
      attributes to change. 
     o Validation rules are explained above for Lab Number and Lab Date. 
      Title, Author, and Description have no validation. 
     o If no problems are detected, return TRUE. Otherwise return FALSE. 
    . ShowData() - displays all the object's attributes on the console. 

    The main() function and a sample executable is provided. 
*/ 

#include <iostream> 
using namespace std; 
//Class Declaration Section 
class LabMetaData 
{ 
    private: 
    int labNum; 
    string labTitle; 
    string labAuthor; 
    int Month; 
    int Day; 
    int Year; 
    string labDesc;  

    public:  
// LabMetaData(int labNum, string labTitle, string labAuthor,int Month, int Day, int Year, string labDesc); //constructor 
    LabMetaData(int = 0, string = "Empty Title", string = "Empty Author",int = 01, int = 01, int = 2012, string = "Empty Description"); 
    void LabMetaData::SetData(int, string, string, int, int, int, string); 
    void LabMetaData::ShowData(); 
}; 
//Class Implementation Section 
LabMetaData::LabMetaData(int Num, string Title, string Author, int MM, int DD, int YYYY, string Desc) 
{ 
    labNum = Num; 
    labTitle = Title; 
    labAuthor = Author; 
    Month = MM; 
    Day = DD; 
    Year = YYYY; 
    labDesc = Desc; 
} 

void LabMetaData::SetData(int Num, string Title, string Author, int MM, int DD, int YYYY, string Desc) 
{ 
// labNum = 7; 
// labTitle = "N/A"; 
// labAuthor = "Unknown"; 
// Month = 01; 
// Day = 01; 
// Year = 2012; 
// labDesc = "N/A"; 
// return; 
    labNum = Num; 
    labTitle = Title; 
    labAuthor = Author; 
    Month = MM; 
    Day = DD; 
    Year = YYYY; 
    labDesc = Desc; 
    return; 
} 

void LabMetaData::ShowData() 
{ 
    cout << "Lab " << labNum << ": " << labTitle << endl; 
    cout << "Created by: " << labAuthor << endl; 
    cout << "Date: " << Month << "/" << Day << "/" << Year << endl; 
    cout << "Description: " << labDesc << endl; 
    cout << endl; 

    return; 
} 

int main() 
{ 

    LabMetaData Lab7; 

    cout << endl << "Uninitialized: " << endl; 
    Lab7.ShowData(); 

    Lab7.SetData(7, 
    "Introduction to Classes", 
    "Alexander Yhap", 
    10, 3, 2010, 
    "In this lab you will create a new class called LabMetaData. Objects of this class could be used in future lab assignments to store information about the lab itself."); 

    cout << endl << "Intialized: " << endl; 
    Lab7.ShowData(); 

    if(!Lab7.SetData(-1, "Test", "Test", 13, 32, 11, "Causing Errors")) 
     cout << "\nErrors!" << endl; 

    cout << endl << "After Invalid Modification Attempt: " << endl; 
    Lab7.ShowData(); 

    cout << endl << endl; 
    system("pause"); 
    return 0; 
} 

错误消息是:

prog.cpp:32:27: error: no 'void LabMetaData::SetData()' member function declared in class 'LabMetaData' 
prog.cpp:44:28: error: no 'void LabMetaData::ShowData()' member function declared in class 'LabMetaData' 
prog.cpp: In function 'int main()': 
prog.cpp:58:17: error: no matching function for call to 'LabMetaData::LabMetaData()' 
prog.cpp:21:1: note: candidates are: LabMetaData::LabMetaData(int, std::string, std::string, int, int, int, std::string) 
prog.cpp:5:1: note:     LabMetaData::LabMetaData(const LabMetaData&) 
prog.cpp:61:10: error: 'class LabMetaData' has no member named 'ShowData' 
prog.cpp:63:10: error: 'class LabMetaData' has no member named 'SetData' 
prog.cpp:66:10: error: 'class LabMetaData' has no member named 'ShowData' 
prog.cpp:68:9: error: 'Lab4' was not declared in this scope 
prog.cpp:72:10: error: 'class LabMetaData' has no member named 'ShowData' 
+4

你卡在哪里?代码是否编译?如果不是,那么编译器错误是什么? – 2012-04-04 17:31:25

+0

你应该告诉_us_问题是什么,而不是相反。什么是完整的编译器错误信息?如果您使用Visual Studio,那是在“输出”窗口中,而不是“错误”窗口。 – 2012-04-04 17:31:43

+2

我通过[gcc-4.5.1](http://ideone.com/OraVZ)运行了您的代码,并将这些错误消息添加到了您的问题中。 – 2012-04-04 17:32:56

您需要添加类定义中的方法声明。有一种情况不匹配:

class LabMetaData 
{ 
//.... 
    void setData(int, string, string, int, int, int, string); // should be SetData 
    void showData(); // should be ShowData 
}; 

你还缺少该类的默认构造函数:

class LabMetaData 
{ 
//.... 
    LabMetaData(); // <-- default constructor 
}; 
因此

你不能做:

LabMetaData Lab7; 

因为它试图调用缺少的默认构造函数。可以定义一个参数或将参数传递给构造函数。

+0

我在那里看到它们,OP在main中调用它时只使用小写字母来开始名称和大写字母。 – 2012-04-04 17:35:27

+0

@EdS。是的,他也缺少一个默认的构造函数。 – 2012-04-04 17:37:25

+0

好吧,我认为我的班级现在很好走! – user1087935 2012-04-04 17:44:33

C++区分大小写。 setDataSetData不一样。您需要按照您的定义调用它。

+0

谢谢!我总是有这个问题,我必须在代码中更多地关注像这样的简单错误! – user1087935 2012-04-04 20:20:44

(1)第一个错误信息:error: no 'void LabMetaData::SetData()' member function declared in class 'LabMetaData'意味着你应该看看真正贴近你的功能setData,并确保它们匹配:

void    setData(int, string, string, int, int, int, string); 
        |  | 
void LabMetaData::SetData() 

我注意到在这里,你得到了资本错了,在定义,没有参数。这些必须匹配。你的showData函数也有大写错误。

(2)错误消息:error: no matching function for call to 'LabMetaData::LabMetaData()'意味着您的类没有收到自动默认构造函数(因为您给它一个需要参数的构造函数),所以它不知道如何在行中正确创建一个:

LabMetaData Lab7; 

所以你要么有参数构造这一点,或者提供一个默认的构造函数:

void LabMetaData::LabMetaData() { 
    //stuff 
} 

所有你的错误的其余部分是由于这两个问题。

(3)回应您的意见,你的错误有关Lab4,因为该行的:

if(!Lab4.SetData(-1, "Test", "Test", 13, 32, 11, "Causing Errors")) 

但是你从来没有创建了一个名为Lab4的对象。你是否打算在对象Lab7上调用函数?另外,你说SetData函数不应该返回任何东西。我不知道如何在没有抛出异常的情况下失败,所以我认为你根本不需要这个if声明。

+0

好的,所以我现在开始上课了。这只是宣布我的课程并将其称为实验7 – user1087935 2012-04-04 17:49:01

+0

在int Main中获取什么主题的问题 – user1087935 2012-04-04 17:49:25

+0

@ user1087935:我编辑了问题以解决该问题。 – 2012-04-04 17:52:46