how come this happen? thank you very much
-
I build a dialog based MFC program. in the file "StdAfx.h" I add the following code,which is used to define a global variable of type
CString
.#ifndef jjjjjjjjjjjjjjjjjjj
#define jjjjjjjjjjjjjjjjjjj
CString StrGlobal;
#endifand then I use it in another file "newfile.cpp"
#include "stdafx.h"
extern CString StrGlobal;
void fun()
{
StrGlobal = _T("KDKDK");
}when I build the program, following link error shows up: Linking... StdAfx.obj : error LNK2005: "class CString StrGlobal" (?StrGlobal@@3VCString@@A) already defined in newfile.obj why dose this happen? and how can I fix it ? Thank you very much!!! ------------------- I am learning C++ and English
-
I build a dialog based MFC program. in the file "StdAfx.h" I add the following code,which is used to define a global variable of type
CString
.#ifndef jjjjjjjjjjjjjjjjjjj
#define jjjjjjjjjjjjjjjjjjj
CString StrGlobal;
#endifand then I use it in another file "newfile.cpp"
#include "stdafx.h"
extern CString StrGlobal;
void fun()
{
StrGlobal = _T("KDKDK");
}when I build the program, following link error shows up: Linking... StdAfx.obj : error LNK2005: "class CString StrGlobal" (?StrGlobal@@3VCString@@A) already defined in newfile.obj why dose this happen? and how can I fix it ? Thank you very much!!! ------------------- I am learning C++ and English
-
yes, I make it static and it works. thank you so much. but ,could you please explain to me briefly the reason? ------------------- I am learning C++ and English
The .h file might have been include by multiple cpp file. so while compiling , the variable will be present in the vtable of obj file corresponding to each cpp. so while linking, linker will find multiple object and generates the error since it dosen't know to which obj it have to link. nave
-
I build a dialog based MFC program. in the file "StdAfx.h" I add the following code,which is used to define a global variable of type
CString
.#ifndef jjjjjjjjjjjjjjjjjjj
#define jjjjjjjjjjjjjjjjjjj
CString StrGlobal;
#endifand then I use it in another file "newfile.cpp"
#include "stdafx.h"
extern CString StrGlobal;
void fun()
{
StrGlobal = _T("KDKDK");
}when I build the program, following link error shows up: Linking... StdAfx.obj : error LNK2005: "class CString StrGlobal" (?StrGlobal@@3VCString@@A) already defined in newfile.obj why dose this happen? and how can I fix it ? Thank you very much!!! ------------------- I am learning C++ and English
ewighell wrote:
#ifndef jjjjjjjjjjjjjjjjjjj #define jjjjjjjjjjjjjjjjjjj CString StrGlobal; #endif
#ifndef jjjjjjjjjjjjjjjjjjj
#define jjjjjjjjjjjjjjjjjjj
extern CString StrGlobal;
#endifewighell wrote:
#include "stdafx.h" extern CString StrGlobal; void fun() { StrGlobal = _T("KDKDK"); }
#include "stdafx.h"
CString StrGlobal;
void fun()
{
StrGlobal = _T("KDKDK");
}
Nibu thomas Software Developer