我正在尝试学习如何在 C++ 中使用外部类文件并且碰壁了。一切都在xcode中运行得很好,但是当试图在命令行中运行它时,我得到了以下错误。
来自 g++:
架构 x86_64 的未定义符号:“GradeBook::GradeBook(std::basic_string, std::allocator >)”,引用自:cc9lOO3b 中的 _main.o “GradeBook::getCourseName() const”,引用自:cc9lOO3b 中的 _main。 o ld:未找到架构 x86_64 的符号 collect2:ld 返回 1 退出状态
这是该类的源代码:
// GradeBook.h header file
#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
// constructor initializes couseName with string supplied as argument
GradeBook::GradeBook ( std::string name )
: courseName ( name ) // member initializer to initialize courseName
{
// empty body
} // end GradeBook constructor
// function that sets the course name
void GradeBook::setCourseName ( std::string name )
{
courseName = name; // store the course name in the objec
} // end function setCourseName
// function that gets the course name
std::string GradeBook::getCourseName() const
{
return courseName; // returns the object's courseName
} // end function getCourseName
// function that displays a welcome message to the GradeBook user
void GradeBook::displayMessage() const
{
// this statement calls getCourseName to get the
// name of the course this Gradebook represents
std::cout << "Welcome to the grade book for\n" << getCourseName() << "!" << std::endl;
} // end function displayMessage
谢谢参观!