2013-04-22 92 views
1

我試圖探索命名空間和試圖建立一個樣本庫我C.做的方式我從來沒有在C++中做到了這一點,所以這裏是我在做什麼。這是我的代碼工作正常,我想放在單獨的文件。C++庫創建使用命名空間

#include <iostream> 
#include <string> 

namespace TCS{ 
    class employee{ 
     int uID; 
     std::string name; 

     public: 
     employee(const int& uId, const std::string& name) 
     { 
      this->uID=uId; 
      (this->name).assign(name); 
      } 
     int getID() 
     { 
      return uID; 
      } 
     std::string getName() 
       { 
        return name; 
      } 
     }; 
    } 

using namespace std; 

class employee{ 
     int uID; 
     string name; 

     public: 
     employee(const int& uId, const string& name) 
     { 
      this->uID=uId; 
      (this->name).assign(name); 
      } 
     int getID() 
     { 
      return uID; 
      } 
     string getName() 
       { 
        return name; 
      } 
     }; 


int main() 
{ 

    employee TechMEmp1(1,"Andrew"); 

    TCS::employee TCSEmp1(1,"Thomas"); 

    cout << "Tech M Employee is :- ID : " << TechMEmp1.getID() << " Name : " 
    << TechMEmp1.getName() << endl; 

    cout << "TCS Employee is :- ID : " << TCSEmp1.getID() << " Name : " 
    << TCSEmp1.getName() << endl; 

    return 0; 
    } 

現在我只是想複製我把內容在不同的C.文件的方式我cteated與內容的文件TCS.h:

#ifndef TCS_HEADER 
#define TCS_HEADER 
namespace TCS{ 
    class employee{ 
     int uID; 
     std::string name; 
     public: 
     employee(const int& uId, const std::string& name); 
     int getID(); 
     std::string getName(); 
     }; 
    } 
#endif 

另一個文件:TCSNamespace.cpp與內容

#include "TCS.h" 
#include <string> 

TCS::employee(const int& uId, const std::string& name); 
     { 
      this->uID=uId; 
      (this->name).assign(name); 
      } 
int TCS::getID(); 
     { 
      return uID; 
      } 
std::string TCS::getName(); 
     { 
      return name; 
      } 

而且其中包含主我最後的文件是:

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


using namespace std; 

class employee{ 
     int uID; 
     string name; 

     public: 
     employee(const int& uId, const string& name) 
     { 
      this->uID=uId; 
      (this->name).assign(name); 
      } 
     int getID() 
     { 
      return uID; 
      } 
     string getName() 
       { 
        return name; 
      } 
     }; 


int main() 
{ 

    employee TechMEmp1(1,"Andrew Thomas"); 

    TCS::employee TCSEmp1(1,"Thomas Hula"); 

    cout << "Tech M Employee is :- ID : " << TechMEmp1.getID() << " Name : " 
    << TechMEmp1.getName() << endl; 

    cout << "TCS Employee is :- ID : " << TCSEmp1.getID() << " Name : " 
    << TCSEmp1.getName() << endl; 

    return 0; 
    } 

我正在使用Cygwin並嘗試單獨編譯文件,當我輸入命令時: g ++ -Wall -c TCSNamespace.cpp(我認爲這會編譯我的TCSNamespace.cpp編譯c文件的方式) 我得到:

$ g++ -Wall -c TCSNamespace.cpp 
In file included from TCSNamespace.cpp:1: 
TCS.h:6: error: using-declaration for non-member at class scope 
TCS.h:6: error: expected `;' before "name" 
TCS.h:8: error: expected unqualified-id before '&' token 
TCS.h:8: error: expected `,' or `...' before '&' token 
TCS.h:8: error: ISO C++ forbids declaration of `parameter' with no type 
TCS.h:10: error: using-declaration for non-member at class scope 
TCS.h:10: error: expected `;' before "getName" 
TCSNamespace.cpp:4: error: expected constructor, destructor, or type conversion before ';' token 
TCSNamespace.cpp:5: error: expected unqualified-id before '{' token 
TCSNamespace.cpp:5: error: expected `,' or `;' before '{' token 
TCSNamespace.cpp:9: error: `int TCS::getID()' should have been declared inside `TCS' 
TCSNamespace.cpp:10: error: expected unqualified-id before '{' token 
TCSNamespace.cpp:10: error: expected `,' or `;' before '{' token 
TCSNamespace.cpp:13: error: `std::string TCS::getName()' should have been declared inside `TCS' 
TCSNamespace.cpp:14: error: expected unqualified-id before '{' token 
TCSNamespace.cpp:14: error: expected `,' or `;' before '{' token 

我打算做的是分別編譯TCSNamespace.cpp第二Namespace.cpp,然後將它們鏈接獲得可執行下面的命令:

g++ -Wall -c TCSNamespace.cpp 
g++ -Wall -c Namespace.cpp 
gcc TCSNamespace.o Namespace.o –o Namespace 

誰能告訴我要去哪裏錯了?我也想知道在C++中使用C++創建「TCS.h」是一種很好的做法,因爲C++使用頭的類型。

感謝

+1

您仍然需要在各自的頭文件中聲明您的類的依賴關係。不要依賴包含源文件(.cpp)來爲你包含它們。例如:'TCS.h'從哪裏得到'std :: string'的定義?這是在你的.cpp文件的系統頭文件之前包含你的頭文件通常是好的做法的原因之一,因此,除非滿足依賴關係,否則將無法編譯。 – WhozCraig 2013-04-22 05:36:45

回答

2

TCS.h需要#include <string>

當前TCS.h包含在<string>之前,因此std::string的聲明在處理name時未知。

#ifndef TCS_HEADER 
#define TCS_HEADER 
#include <string> 
namespace TCS{ 
    class employee{ 
     int uID; 
     std::string name; 
     public: 
     employee(const int& uId, const std::string& name); 
     int getID(); 
     std::string getName(); 
     }; 
    } 
#endif 

此外,@ForEverR打我給它的.cpp內容,所以我就注意到你可以寫:

TCS::employee::employee(const int& uId, const std::string& name); 
{ 
    this->uID=uId; 
    (this->name).assign(name); 
} 

最後:

我也想知道在C++中創建「TCS.h」的方式是在 C中完成的,這是很好的做法,因爲C++使用頭的類型。

答:是的。

2

TCS.h需要包括<string>

TCSNamespace.cpp應該

#include "TCS.h" 
#include <string> 

namespace TCS { 

employee::employee(const int& uId, const std::string& name) 
     { 
      this->uID=uId; 
      (this->name).assign(name); 
     } 
int employee::getID() 
     { 
      return uID; 
     } 
std::string employee::getName() 
     { 
      return name; 
     } 
} 

在你的情況 - 你不指定,函數是類的成員。

3

一個問題,我看到的是,你是不是在聲明.cpp文件中的函數類員工中的一員。你需要用命名空間來包裝你的整個.cpp文件,然後在每個成員函數的前面加上類名稱。您也可以在名稱空間TCS::employee::employee(...)之前加入每個函數,而不是像下面那樣用命名空間來包裝整個代碼,但這會變得非常繁瑣,以至於不斷寫出所有內容。還要注意函數定義中的分號。

#include "TCS.h" 
#include <string> 

namespace TCS { 
    employee::employee(const int& uId, const std::string& name) // <-- remove semicolon 
    { 
     this->uID=uId; 
     (this->name).assign(name); 
    } 
    int employee::getID() // <-- remove semicolon 
    { 
     return uID; 
    } 
    std::string employee::getName() // <-- remove semicolon 
    { 
     return name; 
    } 
} 

此外,您還需要從函數定義的末尾刪除分號,如上所示。

我還建議在TCS.h中包含string,因爲任何包含TCS.h的文件都必須包含字符串。

+0

道歉錯誤地編輯你的答案;糾正。 – Keith 2013-04-22 05:44:21

+0

這一切都很好。我只是有點困惑。 – Danny 2013-04-22 05:51:44