Visual studio 2010 为什么可以';t我在未导入的文件中有重复的定义吗?
假设我有五个文件;fileA.cpp、fileA.hpp、fileB.cpp、fileB.hpp和main.cpp。两个头文件都定义函数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(
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;