使用命名空间创建c++库
C++ Library Creation using namespace
我正在尝试探索命名空间,并尝试创建一个示例库,我在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中把内容放在单独文件中的方式。我创建了一个文件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;
}
最后一个包含main的文件是:
#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 tcsnamspace .cpp(我认为这会像编译c文件一样编译我的tcsnamspace .cpp)我:
$ 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
我打算做的是分别编译tcsnamespaces .cpp和namespaces .cpp,然后用下面的命令链接它们以获得可执行文件:
g++ -Wall -c TCSNamespace.cpp
g++ -Wall -c Namespace.cpp
gcc TCSNamespace.o Namespace.o –o Namespace
谁能告诉我哪里错了?我还想知道在c++中创建"TCS.h"的方式是在C中完成的,这是一个很好的实践,因为c++使用头文件类型。
谢谢
我看到的一个问题是,您没有将.cpp文件中的函数声明给类employee的成员。您需要用名称空间包装整个.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的文件都必须包含字符串include.
TCS.h
需要#include <string>
目前,TCS.h
包含在<string>
之前,所以在处理name
时,std::string
的声明是未知的。
#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++使用头的类型。
回答:是的。
TCS.h需要包含<string>
header
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;
}
}
在你没有指定的情况下,函数是类的成员
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 试图在visual studio上用C++创建一个桌面应用程序
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何在C++20中创建模板别名的推导指南
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 链接到自行创建的dll失败
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++类内存结构中创建"spacer"?
- 终端不会为C++文件创建.exe文件吗
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息