我有 3 个文件 Test.h 、 Test.cpp 和 main.cpp
Test.h
#ifndef Test_H
#define Test_H
namespace v
{
int g = 9;;
}
class namespce
{
public:
namespce(void);
public:
~namespce(void);
};
#endif
Test.cpp
#include "Test.h"
namespce::namespce(void)
{
}
namespce::~namespce(void)
{
}
Main.cpp
#include <iostream>
using namespace std;
#include "Test.h"
//#include "namespce.h"
int main ()
{
return 0;
}
在构建过程中出现以下错误..
1>namespce.obj : error LNK2005: "int v::g" (?g@v@@3HA) already defined in main.obj
1>C:\Users\E543925\Documents\Visual Studio 2005\Projects\viku\Debug\viku.exe : fatal error LNK1169: one or more multiply defined symbols found
请尽快帮助..
这是一个定义:
namespace v
{
int g = 9;
}
被复制到main.obj
and test.obj
因为#include "Test.h"
在每个.cpp
文件。包括守卫#ifndef Test_H
仅防止单个翻译单元中的多个包含。
改成:
namespace v
{
extern int g; // This is now a declaration and extern tells the compiler
// that there is definition for g somewhere else.
}
并将以下内容添加到Test.cpp
:
namespace v
{
int g = 9; // This is now the ONLY definition of 'g', in test.obj.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)