1

我正在尝试探索命名空间并尝试像在 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 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++ 中创建“TCS.h”的方式与在 C 中完成的方式相同,这是一种好的做法,因为 C++ 使用标头类型。

谢谢

4

3 回答 3

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;
    }
}

您还需要从函数定义的末尾删除分号,如上所示。

我还建议包含string在 TCS.h 中,因为任何包含 TCS.h 的文件都必须包含字符串。

于 2013-04-22T05:38:19.703 回答
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;
        }
}

在您的情况下-您没有指定,该函数是类的成员。

于 2013-04-22T05:35:00.853 回答
2

TCS.h需要#include <string>

目前,TCS.h 包含在之前,因此在处理时不知道<string>的声明。std::stringname

#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++ 使用标头类型。

回答:是的。

于 2013-04-22T05:36:04.417 回答