2017-07-17 93 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工作。 –