代码在xcode中运行良好,但在命令行中出错 我试图学习如何在C++中使用外部类文件并打到墙上。所有东西在xcode中都运行得很快,但是当试图在命令行中运行它时,我得到了以下错误

代码在xcode中运行良好,但在命令行中出错 我试图学习如何在C++中使用外部类文件并打到墙上。所有东西在xcode中都运行得很快,但是当试图在命令行中运行它时,我得到了以下错误,c++,xcode,command-line,g++,C++,Xcode,Command Line,G++,来自g++: // 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 ) : courseNam

来自g++:

// 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
架构x86_64的未定义符号: “成绩册::成绩册(标准::基本字符串,标准::分配器>)”,参考自: _CC93B.o中的干管 “成绩册::getCourseName()常量”,引用自: _CC93B.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
//成绩册.h头文件
#包括
#包括“成绩册.h”//包括班级成绩册的定义
//构造函数使用作为参数提供的字符串初始化couseName
成绩册::成绩册(标准::字符串名称)
:courseName(name)//初始化courseName的成员初始值设定项
{
//空体
}//结束成绩册构造函数
//设置课程名称的函数
无效成绩册::setCourseName(标准::字符串名称)
{
courseName=name;//将课程名称存储在对象中
}//结束函数setCourseName
//获取课程名称的函数
std::string成绩册::getCourseName()常量
{
return courseName;//返回对象的courseName
}//结束函数getCourseName
//向成绩册用户显示欢迎信息的函数
作废成绩册::displayMessage()常量
{
//此语句调用getCourseName以获取
//这本成绩册代表的课程名称
你忘了吗

#include <string>
#包括

xcode可以从
中间接包含它,但它不是强制要求的,所以请您自己包含它以确保安全。

您不能只编译一个源文件,您需要编译所有源文件。最简单的方法是将命令行上的所有源文件传递到
g++

g++ main.cpp GradeBook.cpp # Other flags (e.g. "-o OutputFile", "-Wall", etc.)
如果只编译
main.cpp
,您将看到在
GradeBook.cpp
中定义的任何符号的未定义符号错误,例如
GradeBook::GradeBook()
constructor。相反,如果您只编译
GradeBook.cpp
,您将看到
main.cpp
中定义的任何符号的未定义符号错误,即
main()
函数

此命令行在每次运行时都会重新编译每个源文件。对于像这样的小项目,这很好,因为您不会注意到编译时间,但随着项目的增长,您会发现只重新编译已更改的文件或包含已更改的头文件更方便。您通常会使用依赖项跟踪器,例如h正如GNU所做的那样。在进行依赖关系分析后,它将一次重新编译一个源文件,如下所示:

g++ main.cpp -c -o main.o $(CXXFLAGS)  # Compile main.cpp
g++ GradeBook.cpp -c -o GradeBook.o $(CXXFLAGS)  # Compile GradeBook.cpp

g++ main.o GradeBook.o $(LDFLAGS)  # Link two object files into executable
当然,您也可以手动执行此操作,但一次将所有源文件传递到
g++
要简单得多,而且它可以一起进行编译和链接


正如Luchian Grigore所提到的,在代码源>文件中使用<>代码>使用< <代码> >:STD::String 类。通常,这样做会导致编译器错误,但是C++标准库的实现正好是<>代码>包含< <代码> >代码> >代码>。如果将代码移植到其他平台,很可能无法编译,因此最好首先避免这种情况。但即使在没有包含的情况下成功编译,也不会导致出现链接器错误。

谢谢!这消除了一些错误,但我仍然得到了这个错误:未定义的符号用于架构x86_64:“_main”,引用自:crt1.10.6中的开始。ld:symbol找不到体系结构x86_64 collect2:ld返回1个出口status@ChrisRukan非常感谢@AdamRosenfield和@LuchianGrigore!显然我是这方面的新手,非常感谢您的帮助。请发布给出错误的g++命令。