使用命名空间创建c++库

C++ Library Creation using namespace

本文关键字:c++ 创建 命名空间      更新时间:2023-10-16

我正在尝试探索命名空间,并尝试创建一个示例库,我在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;
        }
}

在你没有指定的情况下,函数是类的成员