【发布时间】:2011-08-26 14:06:46
【问题描述】:
我正在阅读 Deitel 关于 C++:如何编程的书。
在一个特定的部分(第 3.9 节)中,他们解释了接口和实现的概念。他们还提供了巩固这一概念的示例代码。
虽然我或多或少地理解了接口和实现分离背后的基本原理,但我无法执行示例代码。示例代码由 3 个文件组成:
1) GradeBook.h
// GradeBook.h
// GradeBook class definition. This file presents GradeBook's public
// interface without revealing the implementations of GradeBook's member
// functions, which are defined in gradebook.3.12.cpp
#include <string> // class GradeBook uses C++ standard string class
using std::string;
// 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
}; // end class GradeBook
2) 成绩册.3.12.cpp
// Fig 3.12: GradeBook.cpp
// GradeBook member-function definitions. This file contains
// implementations of the member functions prototyped in GradeBook.h
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include "GradeBook.h"
// 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::getCourseName()
{
return courseName; // return object's courseName
}// end function getCourseName
// display a welcome message to the GradeBook user
void GradeBook::displayMessage()
{
// call getCourseName to get the courseName
cout << "Welcome to the grade book for\n" << getCourseName()
<< "!" << endl;
} // end function displayMessage
3) 成绩册.3.13.cpp
// GradeBook class demonstration after separating its interface from its implementation
#include <iostream>
using std::cout;
using std::endl;
#include "GradeBook.h" // include definition of class GradeBook
// function main begins program execution
int main()
{
// create two GradeBook objects
GradeBook gradeBook1(" CS101 Intro 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;
return 0;
}
要执行和获得类似于 Deitel 书中所示的输出,我必须运行文件号 3。o/p 应该是“CS101 Intro to C++ programming”,然后是“CS101 Intro to C++ programming”。换行符。
但是在运行此文件时,我收到以下错误消息 -
Undefined symbols:
"GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>,std::allocator<char> >)", referenced from:
_main in ccaOv3rj.o
_main in ccaOv3rj.o
"GradeBook::getCourseName()", referenced from:
_main in ccaOv3rj.o
_main in ccaOv3rj.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
请解释发生了什么。我查看了有关接口和实现的其他帖子 - 发布解决方案的 ppl 说必须包含主要功能。但我认为这不是这里的问题。这里还有一些我想不通的事情……可能是因为我不是 CS 专业的。
新人,非常感谢。
【问题讨论】:
-
您的链接器告诉您它在班级成绩簿中找不到已编译的方法。您使用的是什么开发环境?
-
巴特沃思先生提供了解决方案。我正在使用 g++-4.0
-
也许这会在你的书中稍后介绍,但建议在你的标题中放置一个包含保护。在这种情况下,这不是问题,所以不要担心。 (别忘了接受下面的答案。)
标签: c++ interface implementation