简体   繁体   English

简单的c ++代码无法编译(链接器命令失败,退出代码为1)

[英]simple c++ code does not compile (linker command failed with exit code 1)

I'm new to C++ and studying the Dietel book. 我是C ++的新手,正在学习Dietel的书。 On the book, it has some example codes for classes and interfaces 在书上,它有一些关于类和接口的示例代码

Gradebook.h 成绩簿

#ifndef GradeBook_h
#define GradeBook_h


#endif /* GradeBook_h */


#include <string>

class GradeBook
{
    public:
    explicit GradeBook( std::string );
    void setCourseName( std::string );
    std::string getCourseName() const;
    void displayMessage() const;

private:
    std::string courseName;
};

Gradebook.cpp 成绩簿

#include <iostream>
#include "GradeBook.h"

using namespace std;


GradeBook::GradeBook( string name )
{
    courseName = name;
}

void GradeBook::setCourseName( string name )
{
    courseName = name;
}

string GradeBook::getCourseName() const
{
    return courseName;
}

void GradeBook::displayMessage() const
{
    std::cout << "Welcome to the grade book for " << getCourseName() << std::endl;
}

main.cpp main.cpp

#include <iostream>
#include "GradeBook.h"

using namespace std;

int main()
{
    GradeBook gradeBook1("CS 101 Introduction to C++ Programming");
    GradeBook gradeBook2("CS 102 Data Structures in C++");

    cout << "gradeBook1 : " << gradeBook1.getCourseName() << endl;
    cout << "gradeBook2 : " << gradeBook2.getCourseName() << endl;

}

So, I am trying to compile this on my mac terminal using g++ main.cpp -o example.out . 所以,我试图使用g++ main.cpp -o example.out在我的mac终端上编译它。 But it seems that this constantly gives me an error saying that 但这似乎不断给我一个错误,说

Undefined symbols for architecture x86_64: "GradeBook::GradeBook(std::__1::basic_string, std::__1::allocator >)", referenced from: _main in main-0602c7.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 体系结构x86_64的未定义符号:“ GradeBook :: GradeBook(std :: __ 1 :: basic_string,std :: __ 1 :: allocator>)”,引用自:main-0602c7.o ld中的:找不到以下符号:体系结构x86_64 clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)

I have tried getting rid of most of the function declarations and function implementations except for the constructor and the member variable, but it seems to be giving me the same error still. 我试图摆脱除构造函数和成员变量以外的大多数函数声明和函数实现,但是它似乎仍然给我同样的错误。

I think I copied the code exactly from the book, but I do not understand what I am doing wrong. 我想我完全是从书中复制了代码,但是我不明白我在做什么错。 Any help would be appreciated. 任何帮助,将不胜感激。

You'll have to compile all sources, so add your GradeBook class implementation too 您必须编译所有源代码,因此也要添加GradeBook类实现

g++ main.cpp GradeBook.cpp -o example.out
             ~~~~~~~~~~~~~

As already mentioned in above example, you have to mention both source files (main.cpp and GradeBook.cpp) while compiling the code. 如上面的示例中已经提到的,在编译代码时,您必须同时提及两个源文件(main.cpp和GradeBook.cpp)。

This will work. 这将起作用。

However, there is one more potential problem in your code. 但是,您的代码中还有一个潜在的问题。

#ifndef GradeBook_h
#define GradeBook_h


#endif /* GradeBook_h */

There is no code inside ifndef-endif guard. ifndef-endif保护内部没有代码。 You need to put the complete code in .h file inside ifndef-endif. 您需要将完整的代码放在ifndef-endif内的.h文件中。 Otherwise when you work on a larger project and GradeBook.h is getting included from multiple places, you might get redeclaration error. 否则,当您处理较大的项目时,并且从多个位置包含GradeBook.h时,您可能会收到重新声明错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++ 无法编译简单的类头(链接器命令失败,退出代码为 1) - C++ Can't compile simple class header (linker command failed with exit code 1) C ++链接器命令失败,退出代码为1 - C++ linker command failed with exit code 1 启用 OpenMP 的简单代码无法编译:“clang: 错误:链接器命令失败,退出代码为 1” - Simple OpenMP-enabled code does not compile: "clang: error: linker command failed with exit code 1" C ++链接器错误(“链接器命令失败,退出代码为1”) - C++ Linker Error (“Linker command failed with exit code 1”) 编译错误:链接器命令失败,退出代码为1 - Compile Error: linker command failed with exit code 1 Xcode链接器命令失败,退出代码为1 c ++ - Xcode linker command failed with exit code 1 c++ Xcode C ++错误“链接器命令失败,退出代码为1” - Xcode C++ Error “linker command failed with exit code 1” 链接器命令失败,退出代码为1 C ++ Xcode - linker command failed with exit code 1 C++ Xcode C ++:Linker命令在xcode中的退出代码为-1 - C++:Linker command with exit code -1 in xcode 获取错误clang:错误:从终端编译C ++文件时,链接器命令失败,退出代码为1(使用-v查看调用) - Getting error clang: error: linker command failed with exit code 1 (use -v to see invocation) while compile C++ file from terminal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM