我试图探索命名空间和试图建立一个样本库我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++使用头的类型。
感谢
您仍然需要在各自的头文件中声明您的类的依赖关系。不要依赖包含源文件(.cpp)来为你包含它们。例如:'TCS.h'从哪里得到'std :: string'的定义?这是在你的.cpp文件的系统头文件之前包含你的头文件通常是好的做法的原因之一,因此,除非满足依赖关系,否则将无法编译。 – WhozCraig 2013-04-22 05:36:45