Symbol Already defined...
-
I am new to VC++ and MFC and not very much strong on it... I am having this problem. I have a dialog application which contains a ListView control. I want to have the column names in Urdu. so here what I do.
TCHAR szCol7[] = {0x6A9, 0x644, 0x20, 0x646, 0x642, 0x637, 0x6C1, 0x20, 0x62C, 0x627, 0x62A} ;
As my Dialog class, UrduAppDlg contains the ListView contorl, what I do is that place the above mentioned defination of Unicode string in UrduAppDlg.h file before the clas defination... Right? So far so good... But when I build the solution, I get the following error
UrduTextAnalyzer error LNK2005: "wchar_t * szCol7" (?szCol7@@3PA_WA) already defined in UrduTextAnalyzer.obj
But placing the same defination in UrduTextDlg.cpp does not create any problems and I get my job done... Please tell me what this phenomen is and why? Where should I put such data like col names? I cannot use resource compiler I think becuase Its not Unicode compatible I think... Please guide me in this regard. Mohsin Polite Programmer
More Object Oriented then C#
-
I am new to VC++ and MFC and not very much strong on it... I am having this problem. I have a dialog application which contains a ListView control. I want to have the column names in Urdu. so here what I do.
TCHAR szCol7[] = {0x6A9, 0x644, 0x20, 0x646, 0x642, 0x637, 0x6C1, 0x20, 0x62C, 0x627, 0x62A} ;
As my Dialog class, UrduAppDlg contains the ListView contorl, what I do is that place the above mentioned defination of Unicode string in UrduAppDlg.h file before the clas defination... Right? So far so good... But when I build the solution, I get the following error
UrduTextAnalyzer error LNK2005: "wchar_t * szCol7" (?szCol7@@3PA_WA) already defined in UrduTextAnalyzer.obj
But placing the same defination in UrduTextDlg.cpp does not create any problems and I get my job done... Please tell me what this phenomen is and why? Where should I put such data like col names? I cannot use resource compiler I think becuase Its not Unicode compatible I think... Please guide me in this regard. Mohsin Polite Programmer
More Object Oriented then C#
I guess you have accidently included the
.cpp
file instead of the.h
file or you have forgotten to add#pragma once
to the top of the.h
file.
Nibu thomas Software Developer Faqs by Michael dunn
-
I am new to VC++ and MFC and not very much strong on it... I am having this problem. I have a dialog application which contains a ListView control. I want to have the column names in Urdu. so here what I do.
TCHAR szCol7[] = {0x6A9, 0x644, 0x20, 0x646, 0x642, 0x637, 0x6C1, 0x20, 0x62C, 0x627, 0x62A} ;
As my Dialog class, UrduAppDlg contains the ListView contorl, what I do is that place the above mentioned defination of Unicode string in UrduAppDlg.h file before the clas defination... Right? So far so good... But when I build the solution, I get the following error
UrduTextAnalyzer error LNK2005: "wchar_t * szCol7" (?szCol7@@3PA_WA) already defined in UrduTextAnalyzer.obj
But placing the same defination in UrduTextDlg.cpp does not create any problems and I get my job done... Please tell me what this phenomen is and why? Where should I put such data like col names? I cannot use resource compiler I think becuase Its not Unicode compatible I think... Please guide me in this regard. Mohsin Polite Programmer
More Object Oriented then C#
I think the reason is that
szCol7
is defined in a header that is included in multiple .CPP files and thus defined multiple times. Use something like this instead:// In a .CPP file TCHAR szCol7[] = {0x6A9, 0x644, 0x20, 0x646, 0x642, 0x637, 0x6C1, 0x20, 0x62C, 0x627, 0x62A} ; // In a header file extern TCHAR szCol7[];
You include the header file into and .CPP file from which you want to accessszCol7
. Steve -
I am new to VC++ and MFC and not very much strong on it... I am having this problem. I have a dialog application which contains a ListView control. I want to have the column names in Urdu. so here what I do.
TCHAR szCol7[] = {0x6A9, 0x644, 0x20, 0x646, 0x642, 0x637, 0x6C1, 0x20, 0x62C, 0x627, 0x62A} ;
As my Dialog class, UrduAppDlg contains the ListView contorl, what I do is that place the above mentioned defination of Unicode string in UrduAppDlg.h file before the clas defination... Right? So far so good... But when I build the solution, I get the following error
UrduTextAnalyzer error LNK2005: "wchar_t * szCol7" (?szCol7@@3PA_WA) already defined in UrduTextAnalyzer.obj
But placing the same defination in UrduTextDlg.cpp does not create any problems and I get my job done... Please tell me what this phenomen is and why? Where should I put such data like col names? I cannot use resource compiler I think becuase Its not Unicode compatible I think... Please guide me in this regard. Mohsin Polite Programmer
More Object Oriented then C#
yes i agree with Nibu thomas i guess you have forgotten #pragma once in header file or change location szCol7 from header file
-
yes i agree with Nibu thomas i guess you have forgotten #pragma once in header file or change location szCol7 from header file
#pragma once
or not you can define an array twice in two compilation units. Try this for example.// Header "a.h" #pragma once char g_hello[] = "Hello"; // Header "b.h" #pragma once char g_hello[] = "Hello"; // File c.cpp #include "a.h" #include "b.h"
This code will result in "error C2374: 'g_hello' : redefinition; multiple initialization". Steve -
#pragma once
or not you can define an array twice in two compilation units. Try this for example.// Header "a.h" #pragma once char g_hello[] = "Hello"; // Header "b.h" #pragma once char g_hello[] = "Hello"; // File c.cpp #include "a.h" #include "b.h"
This code will result in "error C2374: 'g_hello' : redefinition; multiple initialization". Steveyes I misread this error with another error but...
-
yes I misread this error with another error but...
Thats right, I have #pragma once included at the top of my header, and I am including .h file not the .cpp. However, extern solution worked very well... But the question is that extern only is giving declaratin, not defination... is it? Then is any need to declare the arrays in the header file using extern keyword and then redefine the arrays in a cpp file where they are needed? Please explain a little logic behind this and what role the extern keyword has to play there? Polite Programmer
More Object Oriented then C#
-
Thats right, I have #pragma once included at the top of my header, and I am including .h file not the .cpp. However, extern solution worked very well... But the question is that extern only is giving declaratin, not defination... is it? Then is any need to declare the arrays in the header file using extern keyword and then redefine the arrays in a cpp file where they are needed? Please explain a little logic behind this and what role the extern keyword has to play there? Polite Programmer
More Object Oriented then C#
extern tells your compiler that this symbol is defined elsewhere and not to worry about it if it can't find it in this compilation unit (object code). Basically it passes the problem to the linker. If you have more than one definition for this symbol in separate cpp files the linker will complain that the symbol is already defined. This is what will happen if you try to define your array in more than one cpp file. If you have no definition for this symbol in all your cpp files the linker will complain that it can't find the symbol. In certain cases where it is desirable to have the definition in the header file, you can use __declspec(selectany) to indicate to the linker that if it finds multiple symbols, to just pick one.
-
extern tells your compiler that this symbol is defined elsewhere and not to worry about it if it can't find it in this compilation unit (object code). Basically it passes the problem to the linker. If you have more than one definition for this symbol in separate cpp files the linker will complain that the symbol is already defined. This is what will happen if you try to define your array in more than one cpp file. If you have no definition for this symbol in all your cpp files the linker will complain that it can't find the symbol. In certain cases where it is desirable to have the definition in the header file, you can use __declspec(selectany) to indicate to the linker that if it finds multiple symbols, to just pick one.
A good concise explanation. The only thing missing is to point out that
__declspec(selectany)
isn't portable; but I guess that would be obvious to most as it's a__declspec
. Steve