2017-07-17 67 views
0

我正在测试第一次将类放入单独文件并执行出错时的概念。请帮助这是什么C++程序不执行?

的main.cpp这是主要的文件

#include <iostream> 
    #include <string> 
    #include "newClass.h" 
    using namespace std; 

    int main() 
    { 
     newClass obj1("mayan"); 
     cout << obj1.doneName() << endl ; 


    } 

newClass.h这是单独的头文件

#ifndef NEWCLASS_H 
#define NEWCLASS_H 

#include <iostream> 
#include <string> 
#include <string> 
class newClass{ 

private: 
    string name; 

public: 
    newClass(string z) ; 

    string doneName(); 

}; 


#endif // NEWCLASS_H 

,这是单独newClass.cpp文件

#include "newClass.h" 
#include <iostream> 
#include <string> 

using namespace std; 
newClass::newClass(string z) 
{ 
    name = z ; 
} 

string newClass :: doneName() 
{ 
    return name; 
} 
+0

这不是执行了很多东西。但是,你的意思是它不是编译,不链接,或者不按你想要的方式运行? – Tas

+0

在头文件改变'string'到'的std :: string' –

+0

我的意思是它示出一个错误。 –

回答

2

您需要了解更多关于C++和compilation。阅读更多关于linker

注意,一个C++源文件是一个translation unit并且通常includes一些头文件。详细了解preprocessor

您最好在头文件中使用std::string而不仅仅是string(因为using std;在头文件中被皱眉)。

不要忘记,使所有的警告和编译时的调试信息。用GCC,用g++ -Wall -Wextra -g编译。

实际上,在使用多个翻译单元构建项目时,最好使用一些build automation工具,例如GNU make

请记住,IDE只是荣耀source code editors能够运行外部工具,如构建自动化工具,编译器,调试器,版本控制系统等......您最好能够在命令行上使用这些工具。

+0

谢谢Basile工作。 –