0

假设我有五个文件;fileA.cpp、fileA.hpp、fileB.cpp、fileB.hpp 和 main.cpp。两个头文件都定义了函数void help()。我想在我的 main.cpp 文件中使用 fileA.cpp 和 fileB.cpp 中的函数之间切换。我试过只是切换标题包含,但视觉工作室抱怨重复定义。

4

1 回答 1

1

那是一个链接器错误。将有一个fileB.objfileA.obj(和main.obj)链接在一起以形成可执行文件。fileA.obj并且fileB.obj都将包含 的定义void help(),从而导致多个定义错误。它与更改中的 include 指令无关main.cpp

建议包含void help()在 a 中namespace

文件A.hpp

namespace filea
{
    void help();
}

文件A.cpp

namespace filea
{
    void help()
    {
        // implementation
    }
}

文件B.hpp

namespace fileb
{
    void help();
}

文件B.cpp

namespace fileb
{
    void help()
    {
        // implementation
    }
}

主.cpp

#include <fileA.hpp>
using filea::help;

//#include <fileB.hpp>
//using fileb::help;
于 2012-05-23T07:55:35.647 回答