Ken*_*nta 2 c++ multiple-definition-error
我有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
Run Code Online (Sandbox Code Playgroud)
TEST.CPP
#include "Test.h"
namespce::namespce(void)
{
}
namespce::~namespce(void)
{
}
Run Code Online (Sandbox Code Playgroud)
Main.cpp的
#include <iostream>
using namespace std;
#include "Test.h"
//#include "namespce.h"
int main ()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在建设过程中出现以下错误..
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
Run Code Online (Sandbox Code Playgroud)
请尽快帮助..
这是一个定义:
namespace v
{
int g = 9;
}
Run Code Online (Sandbox Code Playgroud)
由于每个文件中的重复main.obj和重复.包含防护仅在单个翻译单元中防止多个包含.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.
}
Run Code Online (Sandbox Code Playgroud)
并将以下内容添加到Test.cpp:
namespace v
{
int g = 9; // This is now the ONLY definition of 'g', in test.obj.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
889 次 |
| 最近记录: |