2012-04-04 84 views
0

我必須爲作業創建一個類,我已經完成了所有我可以做的工作&我已經完成了研究並閱讀了我的教科書。我還需要做些什麼才能讓我的課程在我的主要課程中完成?你需要知道的一切都在代碼描述中。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

回答

1

您需要添加類定義中的方法聲明。有一種情況不匹配:

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

0

C++區分大小寫。 setDataSetData不一樣。您需要按照您的定義調用它。

+0

謝謝!我總是有這個問題,我必須在代碼中更多地關注像這樣的簡單錯誤! – user1087935 2012-04-04 20:20:44

1

(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