C++中的接口+实现:代码不执行

Interface+Implementation in C++: code does not execute

本文关键字:代码 执行 实现 接口 C++      更新时间:2023-10-16

我正在阅读Deitel关于C++:如何编程的书。

在一个特定的部分(第3.9节),他们解释了接口和实现的概念。他们还提供了巩固这一概念的示例代码。

尽管我或多或少已经了解了接口和实现分离背后的基本原因,但我无法执行示例代码。示例代码由 3 个文件组成:

1) 成绩簿.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 forn" << 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 C++编程入门",后跟换行符上的"CS101 C++编程入门"。

但是在运行此文件时,我收到以下错误消息 -

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

请解释一下发生了什么。我查找了有关界面和实现的其他帖子 - 发布解决方案的人说必须包含主要功能。但我不认为这是这里的问题。这里还有其他事情我无法弄清楚...可能不是CS专业。

纽伊,非常感谢。

猜测,您没有链接通过编译成绩簿.cpp文件生成的 .o 文件。尝试:

g++ gradebook.3.13.cpp gradebook.3.12.cpp -o myprog

此外,在文件名中使用小写字母是个好主意,因此您的标头应该是gradebook.h,而不是GradeBook.h,但这不会导致您收到的错误,该错误来自链接器。文件名中的那些数字(我认为是练习编号)只会长期混淆 - 我会为每个练习创建单独的目录。

看起来你只是在编译gradebook.3.13.cpp,而没有包括gradebook.3.12.cpp中的函数。 这些错误消息意味着链接器找不到 GradeBook 类的函数。

如果您使用的是 g++,请参阅尼尔·巴特沃思的回答,了解有关如何将这两个文件编译并链接到可执行文件的详细信息。 对于其他环境,请查看文档以确定如何将所有源文件添加到"项目"或"工作区"中,或者环境用于将文件收集在一起的任何内容中。