2016-11-11 182 views
0

我在C++编程和一般编程方面很新颖。最近我们已经在课堂上将头文件和其他源文件(实现)添加到我们的程序中。我试过编写最简单的程序,以确保我理解将多个文件包含到一个程序中的基础知识,但它们不会编译,从而导致链接器错误。CodeRunner--无法编译多个源代码的C++程序

即:

Undefined symbols for architecture x86_64: 
    "FooBar::printSomething()", referenced from: 
     _main in main-d52d70.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

当我通过 “g ++ main.cpp中implementation.cpp” 终端编译,一切工作就好了。

我的代码:

的main.cpp

#include "header.h" 

int main() 
{ 
    FooBar object; 
    object.printSomething(); 
    return 0; 
} 

header.h

#ifndef _Header_h_ 
#define _Header_h_ 

#include <iostream> 

using namespace std; 

class FooBar{ 
    public: 
     void printSomething(); 

    private: 
     string helloWorld; 
}; 

#endif 

implementation.cpp

#include "header.h" 

void FooBar::printSomething(){ 
    helloWorld = "Hello World!\n"; 
    cout << helloWorld; 
} 

我喜欢CodeRunner,但这真的让我很沮丧。

谢谢!

+0

您确定您已将'implementation.cpp'添加到叮当中的待链接对象吗?另外,在Header.h中添加'#include ' –

回答

0

对于有过类似的问题人我已经找到了解决办法

我错在制作类的头文件和实现文件在不同的名头文件。

header.h应该被命名为foobar.h中

implementation.cpp应该被命名为foobar.cpp

,当我修改过的文件的名称foobar.h中和foobar.cpp程序编译并在CodeRunner中运行得很好。

我的教授告诉我,一个类的头文件和它的实现文件应该有不同的文件类型,即x.cpp和x.h。