Saturday, May 22, 2010

C++ Linker Error Please Help!?

Error 2 error LNK2005: "char * a" (?a@@3PADA) already defined in TicTacToe .obj Ai.obj





Error 3 error LNK2005: "char * a" (?a@@3PADA) already defined in TicTacToe .obj MainMenu.obj





Error 4 error LNK2019: unresolved external symbol "int __cdecl Menu(void)" (?Menu@@YAHXZ) referenced in function _main TicTacToe .obj





Error 5 fatal error LNK1120: 1 unresolved externals C:\Tic tac test.exe








these are the errors. here is some of the code...





#ifndef MainMenu_H


#define MainMenu_H








char a[9];


int Menu();


void Instructions();


void DisplayBoard(char a[9]);








#endif








int main()


{


int UserInput = 0;


int MoveCount = 0;


int Player1Wins = 0;


int Player2Wins = 0;


int ComputerWins = 0;


int PlayerNumber = 0;


bool Win = false;


bool CatsGame = false;


char c[9] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};


MoveCount = 0;


Menu();





}





See any of the errors??????

C++ Linker Error Please Help!?
The problem is linking issue. You have multiple versions of the variable in the same scope. Put char a[9] inside the main function. Menu() might have the same problem too.





The macro guard looks a little strange too. I don't see the point of it, unless you are including main somewhere else--a definite no no.





I can't go beyond that without the rest of the code. I hope that helps.
Reply:The problem is that you are defining "a" in "mainmenu.h", not just declaring it. This means that "main.cpp" and "mainmenu.cpp" both get their own copies of "a", in violation of the One Definition Rule (ODR). You can fix this, by declaring "a" in "mainmenu.h", and then defining it in only one of the source files. To do this, in "mainmenu.h", write:





extern char a[9];





Then, in one of the source files (either "mainmenu.cpp" or "main.cpp") write:





char a[9];





You may want to see the following related question:


http://answers.yahoo.com/question/index;...


No comments:

Post a Comment