內全局變量我定義變量 'a' 在header.h,並用它在test1.cpp,測試2.cpp。 當我蓋了,我得到了像C++ - 多個文件
致命錯誤LNK1169鏈接錯誤:一個或多個多重定義符號找到
什麼問題? 我想用全局變量'a'可以用在多個cpp中。
header.h
的main.cpp
#include "header.h"
#include "test1.h"
#include "test2.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
test1(); // expected output : 0
test1(); // expected output : 1
test2(); // expected output : 2
test2(); // expected output : 3
cout << "in main : " << a << endl; // expected output : 3
return 0;
}
test1.h
extern int a;
void test1();
test1.cpp
#include "test1.h"
#include "header.h"
void test1() {
cout << "test1 : " << a << endl;
a++;
}
test2.h
extern int a;
void test2();
測試2.cpp
#include "test2.h"
#include "header.h"
void test2() {
cout << "test2 : " << a << endl;
a++;
}
發佈編譯器選項日誌。 – devnull 2013-04-07 09:41:11