Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 2010 为什么可以';t我在未导入的文件中有重复的定义吗?_Visual Studio 2010 - Fatal编程技术网

Visual studio 2010 为什么可以';t我在未导入的文件中有重复的定义吗?

Visual studio 2010 为什么可以';t我在未导入的文件中有重复的定义吗?,visual-studio-2010,Visual Studio 2010,假设我有五个文件;fileA.cpp、fileA.hpp、fileB.cpp、fileB.hpp和main.cpp。两个头文件都定义函数void help()。我想在使用fileA.cpp中的函数和main.cpp文件中的fileB.cpp之间切换。我尝试过切换标题include,但visual studio抱怨定义重复。这是链接器错误。将有一个fileB.obj和fileA.obj(和main.obj)链接在一起以形成可执行文件fileA.obj和fileB.obj都将包含void help(

假设我有五个文件;fileA.cpp、fileA.hpp、fileB.cpp、fileB.hpp和main.cpp。两个头文件都定义函数
void help()
。我想在使用fileA.cpp中的函数和main.cpp文件中的fileB.cpp之间切换。我尝试过切换标题include,但visual studio抱怨定义重复。

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

建议在
命名空间中包含
void help()

fileA.hpp

namespace filea
{
    void help();
}
namespace filea
{
    void help()
    {
        // implementation
    }
}
namespace fileb
{
    void help();
}
namespace fileb
{
    void help()
    {
        // implementation
    }
}
#include <fileA.hpp>
using filea::help;

//#include <fileB.hpp>
//using fileb::help;
文件a.cpp

namespace filea
{
    void help();
}
namespace filea
{
    void help()
    {
        // implementation
    }
}
namespace fileb
{
    void help();
}
namespace fileb
{
    void help()
    {
        // implementation
    }
}
#include <fileA.hpp>
using filea::help;

//#include <fileB.hpp>
//using fileb::help;
文件B.hpp

namespace filea
{
    void help();
}
namespace filea
{
    void help()
    {
        // implementation
    }
}
namespace fileb
{
    void help();
}
namespace fileb
{
    void help()
    {
        // implementation
    }
}
#include <fileA.hpp>
using filea::help;

//#include <fileB.hpp>
//using fileb::help;
文件b.cpp

namespace filea
{
    void help();
}
namespace filea
{
    void help()
    {
        // implementation
    }
}
namespace fileb
{
    void help();
}
namespace fileb
{
    void help()
    {
        // implementation
    }
}
#include <fileA.hpp>
using filea::help;

//#include <fileB.hpp>
//using fileb::help;
main.cpp

namespace filea
{
    void help();
}
namespace filea
{
    void help()
    {
        // implementation
    }
}
namespace fileb
{
    void help();
}
namespace fileb
{
    void help()
    {
        // implementation
    }
}
#include <fileA.hpp>
using filea::help;

//#include <fileB.hpp>
//using fileb::help;
#包括
使用filea::help;
//#包括
//使用fileb::help;