variable problem
-
How can I solve the probelm with the following error message: "variable" already defined in GO.obj
This means that you defined "variable" in more than a file, and both defines it as public If each one should have its copy, try putting static in front of them If the variable should be shared, declare it as extern in one of the files Papa while (TRUE) Papa.WillLove ( Bebe ) ;
-
This means that you defined "variable" in more than a file, and both defines it as public If each one should have its copy, try putting static in front of them If the variable should be shared, declare it as extern in one of the files Papa while (TRUE) Papa.WillLove ( Bebe ) ;
-
I have added extern to the variables with the error message and the problem is solved. Thanks! But i only declare them in one file. Why does the problem occur? my project name is go and i declare the variables in go.h just below #define... (as global)
Anonymous wrote: But i only declare them in one file. But that .h file is included in multiple places.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
I have added extern to the variables with the error message and the problem is solved. Thanks! But i only declare them in one file. Why does the problem occur? my project name is go and i declare the variables in go.h just below #define... (as global)
-
Can you show how and where are you doing the declaration? Papa while (TRUE) Papa.WillLove ( Bebe ) ;
Its ok now. I know the problem. Thank you very much! :-D in go.h: // GO.h : main header file for the GO application // #if !defined(AFX_GO_H__76EC2730_7BFA_4A29_9F6A_226E7AEC7B81__INCLUDED_) #define AFX_GO_H__76EC2730_7BFA_4A29_9F6A_226E7AEC7B81__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols #define MAX_STEP 1000 #define MAX_FILE_NAME 30 #define BOARD_SIZE 19 #define BLACK '@' #define WHITE 'O' #define KEP '*' #define EMPTY '.' #define EMPTY_STAR '+' #define CHECKED_LAND 'C' #define BLACK_LAND 'b' #define WHITE_LAND 'w' #define SHARED_LAND ' ' struct step_record { char command; int row; int col; }; // global variables: static char boardc[BOARD_SIZE][BOARD_SIZE]; static char turn; static int kep_row, kep_col; static int black_eaten, white_eaten; static int step_num, last_step_num; static bool remove_dead_chess; static bool count; static float black_land, white_land; static struct step_record step[MAX_STEP+1]; ///////////////////////////////////////////////////////////////////////////// // CGOApp: // See GO.cpp for the implementation of this class // class CGOApp : public CWinApp { public: ...
-
Its ok now. I know the problem. Thank you very much! :-D in go.h: // GO.h : main header file for the GO application // #if !defined(AFX_GO_H__76EC2730_7BFA_4A29_9F6A_226E7AEC7B81__INCLUDED_) #define AFX_GO_H__76EC2730_7BFA_4A29_9F6A_226E7AEC7B81__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols #define MAX_STEP 1000 #define MAX_FILE_NAME 30 #define BOARD_SIZE 19 #define BLACK '@' #define WHITE 'O' #define KEP '*' #define EMPTY '.' #define EMPTY_STAR '+' #define CHECKED_LAND 'C' #define BLACK_LAND 'b' #define WHITE_LAND 'w' #define SHARED_LAND ' ' struct step_record { char command; int row; int col; }; // global variables: static char boardc[BOARD_SIZE][BOARD_SIZE]; static char turn; static int kep_row, kep_col; static int black_eaten, white_eaten; static int step_num, last_step_num; static bool remove_dead_chess; static bool count; static float black_land, white_land; static struct step_record step[MAX_STEP+1]; ///////////////////////////////////////////////////////////////////////////// // CGOApp: // See GO.cpp for the implementation of this class // class CGOApp : public CWinApp { public: ...
Anonymous wrote: // global variables: static char boardc[BOARD_SIZE][BOARD_SIZE]; static char turn; static int kep_row, kep_col; static int black_eaten, white_eaten; static int step_num, last_step_num; static bool remove_dead_chess; static bool count; static float black_land, white_land; static struct step_record step[MAX_STEP+1]; This is wrong! I believe that you want one variable that is shared among all the components of your application. The current situation is that each object file (component) has its own copy of the variable. So if you execute the following statement in GoApp.cpp:
turn = (char)100
the change is only seen in GoApp.cpp, so in GoView.cpp, the variable turn is still undefined! Try the following instead:// Go.cpp // global variables: char boardc[BOARD_SIZE][BOARD_SIZE]; char turn; extern int kep_row, kep_col; extern int black_eaten, white_eaten; extern int step_num, last_step_num; extern bool remove_dead_chess; extern bool count; extern float black_land, white_land; extern struct step_record step[MAX_STEP+1];
// Go.h // global variables: extern char boardc[BOARD_SIZE][BOARD_SIZE]; extern char turn; int kep_row, kep_col; int black_eaten, white_eaten; int step_num, last_step_num; bool remove_dead_chess; bool count; float black_land, white_land; struct step_record step[MAX_STEP+1];
This way, the value of the variables is the always the same in every component.. Blog[^]