C++ 创建和使用类

标签 c++ class constructor

我必须为一项作业创建一个类(class),我已经做了我能做的一切,我已经完成了研究,并且我已经阅读了我的教科书。我还需要对我的类(class)做些什么才能在我的主要运行中完成什么?您需要知道的一切都在代码说明中。

/*  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'

最佳答案

您需要在类定义中添加方法声明。大小写不匹配:

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; 

因为这会尝试调用缺少的默认构造函数。定义一个或将参数传递给构造函数。

关于C++ 创建和使用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10016295/

相关文章:

c++ - shared_ptr 错误表达式必须有算术、枚举、指针

c++ - 如何使用选项 Kd Ks Ka

c++ - 什么时候类必须在 C++ 类中定义析构函数是必要的,为什么?

python - 具有相同方法名称但不同参数的多重继承会导致类型错误

javascript - 使用 javascript 获取和编辑元素的 css 类

java - 来自 json 或 xml 字符串的对象构造函数

c++ - 修改 TestAssert.h (cppunit) - 为什么包含的顺序对宏扩展很重要?

Java在Android中重用java类

java - 将类设置为 final 和将类构造函数设置为私有(private)有什么区别

C# 构造函数链接说明