2012-01-10 36 views
0

我不明白編譯中的錯誤。我沒有看到任何語法問題。編譯錯誤:找不到頭,類和類接口的任何問題

錯誤:

/dev/shm/ccEF5pIa.o: In function `main': 
fig03_13.cc:(.text+0x45): undefined reference to `GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' 
fig03_13.cc:(.text+0x9d): undefined reference to `GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' 
fig03_13.cc:(.text+0xce): undefined reference to `GradeBook::getCourseName()' 
fig03_13.cc:(.text+0xe1): undefined reference to `GradeBook::getCourseName()' 
collect2: ld returned 1 exit status 

GradeBook.cc:21:8: error: prototype for 'std::string GradeBook::displayMessage()' does not match any in class 'GradeBook' 
GradeBook.h:15:8: error: candidate is: void GradeBook::displayMessage() 

這是GradeBook.h

#include <string> // class GradeBook uses C++ standard string class 
using namespace std; 

// GradeBook class definition 
class GradeBook 
{ 
    public: 
      GradeBook(string); // constructor that initializes courseName 
      void setCourseName(string); // function that sets the course name 
      string getCourseName(); // function that gets the course name 
      void displayMessage(); // function that displays a welcome message 
    private: 
      string courseName; // course name for this GradeBook 
}; 

GradeBook.cc

#include <iostream> 
#include "GradeBook.h" // include definition of class GradeBook 
using namespace std; 

// constructor initializes courseName with string supplied as argument 
GradeBook::GradeBook(string name) 
{ 
    setCourseName(name); // call set function to initialize courseName 
} // end GradeBook constructor 

// function to set the course name 
void GradeBook::setCourseName(string name) 
{ 
    courseName = name; // store the course name in the object 
} // end function setCourseName 

// function to get the course name 
string GradeBook::displayMessage() 
{ 
    // call getCourseName to get the courseName 
    cout << "welcome to the grade book for\n" << getCourseName() << "!" << endl; 
} // end function displayMessage 

fig03_13.cc

#include <iostream> 
#include "GradeBook.h" // include definition of class GradeBook 
using namespace std; 

// function main begins program execution 
int main() { 
    // create two GradeBook objects 
    GradeBook gradeBook1("CS101 Introduction to C++ Programming"); 
    GradeBook gradeBook2("CS102 Data Structures in C++"); 

    // display initial value of courseName for each GradeBook 
    cout << "gradeBook1 created for course: " << gradeBook1.getCourseName() 
     << "\ngradeBook2 created for course: " << gradeBook2.getCourseName() << endl; 
} // end main 
+0

聲明:'void displayMessage();'。定義:'字符串GradeBook :: displayMessage()'。看到問題了嗎? (「getCourseName」的定義在哪裏?) – ildjarn 2012-01-10 23:11:01

回答

0

定義:string GradeBook :: displayMessage()。

聲明:無displayMessage()

你並不需要返回的字符串。只要保持空白版。

+0

這很奇怪。爲什麼我的書會向我展示那樣的代碼呢? – 2012-01-10 23:25:16

+0

也許在你的書中有一個錯字。通常,「顯示」函數返回void,從某種意義上說,業務在其範圍內完成。像getTutorName()這樣的函數可能會返回字符串。您正在從函數請求查找並將其返回給調用者(即字符串GradeBook :: getTutorName – cateof 2012-01-11 13:27:59

相關問題