测试多个文件一行编译,连同测试命名空间,但被程序搞的有点糊涂了,编译报错
测试多个文件一起编译,连同测试命名空间,但被程序搞的有点糊涂了,编译报错。
说下几个文件吧,总共5个文件,
test.h:
A.h
A.app
B.h
B.app
主程序test.cpp
报错:
>B.obj : error LNK2005: "public: static int __cdecl an::TEST::getc(void)" (?getc@TEST@an@@SAHXZ) already defined in A.obj
1>B.obj : error LNK2005: "public: static int __cdecl bn::TEST::getc(void)" (?getc@TEST@bn@@SAHXZ) already defined in A.obj
1>B.obj : error LNK2005: "private: static int an::TEST::c" (?c@TEST@an@@0HA) already defined in A.obj
1>B.obj : error LNK2005: "private: static int bn::TEST::c" (?c@TEST@bn@@0HA) already defined in A.obj
1>test.obj : error LNK2005: "public: static int __cdecl an::TEST::getc(void)" (?getc@TEST@an@@SAHXZ) already defined in A.obj
1>test.obj : error LNK2005: "public: static int __cdecl bn::TEST::getc(void)" (?getc@TEST@bn@@SAHXZ) already defined in A.obj
1>test.obj : error LNK2005: "private: static int an::TEST::c" (?c@TEST@an@@0HA) already defined in A.obj
1>test.obj : error LNK2005: "private: static int bn::TEST::c" (?c@TEST@bn@@0HA) already defined in A.obj
1>E:\CADD\ta1\Debug\ta1.exe : fatal error LNK1169: one or more multiply defined symbols found
怎么避免这样的问题
------解决方案--------------------
去掉a.cpp/b.cpp文件里面的using namespace an/bn
------解决方案--------------------
在test.cpp文件中加入 using namespace an/bn ...
说下几个文件吧,总共5个文件,
test.h:
#ifndef _TEST_H_
#define _TEST_H_
#include <iostream>
namespace an{
class TEST{
private :
static int c;
public :
static int getc();
};
int TEST::c = 10;
int TEST::getc(){
return c;
}
}
namespace bn{
class TEST{
private :
static int c;
public :
static int getc();
};
int TEST::c = 20;
int TEST::getc(){
return c;
}
}
#endif
A.h
#ifndef _A_H_
#define _A_H_
#include "test.h"
class A{
public:
static int getc();
};
#endif;
A.app
#include "A.h"
using namespace bn;
int A::getc(){
return bn::TEST::getc();
}
B.h
#ifndef _B_H_
#define _B_H_
#include "test.h"
class B{
public:
static int getc();
};
#endif;
B.app
#include "B.h"
using namespace an;
int B::getc(){
return an::TEST::getc();
}
主程序test.cpp
#include <iostream>
using namespace std;
#include "test.h"
#include "A.h"
#include "B.h"
int main(){
cout<<"a:"<<A::getc()<<",b:"<<B::getc()<<endl;
cin.get();
}
报错:
>B.obj : error LNK2005: "public: static int __cdecl an::TEST::getc(void)" (?getc@TEST@an@@SAHXZ) already defined in A.obj
1>B.obj : error LNK2005: "public: static int __cdecl bn::TEST::getc(void)" (?getc@TEST@bn@@SAHXZ) already defined in A.obj
1>B.obj : error LNK2005: "private: static int an::TEST::c" (?c@TEST@an@@0HA) already defined in A.obj
1>B.obj : error LNK2005: "private: static int bn::TEST::c" (?c@TEST@bn@@0HA) already defined in A.obj
1>test.obj : error LNK2005: "public: static int __cdecl an::TEST::getc(void)" (?getc@TEST@an@@SAHXZ) already defined in A.obj
1>test.obj : error LNK2005: "public: static int __cdecl bn::TEST::getc(void)" (?getc@TEST@bn@@SAHXZ) already defined in A.obj
1>test.obj : error LNK2005: "private: static int an::TEST::c" (?c@TEST@an@@0HA) already defined in A.obj
1>test.obj : error LNK2005: "private: static int bn::TEST::c" (?c@TEST@bn@@0HA) already defined in A.obj
1>E:\CADD\ta1\Debug\ta1.exe : fatal error LNK1169: one or more multiply defined symbols found
怎么避免这样的问题
------解决方案--------------------
去掉a.cpp/b.cpp文件里面的using namespace an/bn
------解决方案--------------------
在test.cpp文件中加入 using namespace an/bn ...