Visual Studio 2003 linker error LNK2005

Joined
Jul 18, 2006
Messages
1
Reaction score
0
I have one small problem with my project. Let's say thah I am creating new console win32 aplication, and I am using precompiled headers ["stdafx.h"]. Now I create an additional header file with all the data tables I'll be using in my classes called for example "data.h", where I put :
Code:
char _race_names[3][20]={"Human","Ghoul","Mutant"};
and this file I am including in stdafx.h [#include "data.h" etc ;)]
Then I create first class called "foo" [using wizard]. VS created two files : "foo.h" and "foo.cpp". Now I modify a bit these files and finaly :

main.cpp
Code:
#include "stdafx.h"
#include "foo.h"
int main(int argc, char *argv[]) {
foo tmp;
tmp.show();
return 0;
}

stdafx.h
Code:
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include "data.h"
#include <tchar.h>
stdafx.cpp
Code:
#include "stdafx.h"
foo.h
Code:
#pragma once
using namespace std;
class foo {
public :
foo(void);
show(void);
~foo(void);
}
foo.cpp
Code:
#include "stdafx.h"
#include "foo.h"
foo::foo(void){
}
foo::show(void){
cout <<_race_names[0];
}
foo::~foo(void){
}

and that is enough for Visual to throw linker error LNK2005 which says that :
Code:
foo.obj : error LNK2005: "char (* _race_names)[20]" (?_race_name@@3PAY0P@DA) already defined in stdafx.obj
D:\Projects\Fallout\Debug\main.exe : fatal error LNK1169: one or more multiply defined symbols found
any idea how to solve it ? of course this is just an example ... real data.h contains a lot of "const int N_sht", "char _some_name_tables[x][y]", "enum enum_types {bla1,bla2}" etc ...
Why not to implement it in foo.h file ? because in my project are few classes that uses this data so i thougt it should be stored in some external file and then just include it whenewer I need it ... I know that this idea could be crappy ;) then please tell me how can I do it more efficiently

IvaN
 
Joined
Jul 30, 2006
Messages
5
Reaction score
0
See RTFM - it's rulles.
Quote from MSDN (about error LNK2005):
A header file declared and defined a variable. Possible solutions include:

* Declare the variable in .h: extern BOOL MyBool; and then assign to it in a .c or .cpp file: BOOL MyBool = FALSE;.

Your sample:

data.h
Code:
extern char _race_names[3][20];

data.cpp
Code:
char _race_names[3][20]={"Human","Ghoul","Mutant"};
 
Joined
Jul 4, 2006
Messages
11
Reaction score
0
1 ) The method "show()" is missing prototype, you should have declared it as
"void show()" and is not necessary to use (void) as C++ doesn differentiate.

2) Please do "clean" and then "Build".

3) "Voral" has the right suggestion, you cannot use the variable directly just because you have included the file. The variable you have declared has a file scope which you cannot gain access in your "foo" file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top