I am getting LNK error 2005 due to the use of enum in header file. I am not sure what is wrong with it though. Is enum usually included in the header file?
Here is my code. I have 4 files: board.h, board.cpp, Solitaire.h, Solitaire.cpp.
board.h:
#ifndef BOARD_H__
#define BOARD_H__
#include <iostream>
using namespace std;
const int NUM_ROWS = 6;
const int NUM_COLS = 6;
enum PieceType {
HasPiece, NoPiece, Invalid
};
PieceType board_data[NUM_ROWS][NUM_COLS];
#endif
board.cpp:
#include "board.h"
Solitaire.h
#ifndef Solitaire_h__
#define Solitaire_h__
#include "board.h"
#endif
Solitaire.cpp
#include "Solitaire.h"
int main() {
}
The error I get is
Error LNK2005 "enum PieceType (* board_data)[6]"
(?board_data##3PAY05W4PieceType##A) already defined in board.obj
Thank you!
The problem has to do with including definitions in headers. This line
PieceType board_data[NUM_ROWS][NUM_COLS];
defines a new array board_data in each translation unit from which the header is included. To fix this issue, declare the array external, i.e.
extern PieceType board_data[NUM_ROWS][NUM_COLS];
After that, define the array in one of your CPP files.
Note: This problem is not about enum - you would get the same error with any other type.
Related
Note: The question has already been answered here undirectly
The problem is not include guards : they won't help across different
translation units
Note: I know the solution is to use extern keyword.
I'm new to C++. I have a problem understanding #ifndef in header files. When I do something like this, I get an error saying that the variables game_over and turn are already defined.
/*chess.h*/
#ifndef CHESS
#define CHESS
#include <iostream>
#include "chessboard.h"
using namespace std;
bool game_over;
char turn;
chessboard board;
int main();
#endif
/*bishop.cpp*/
#include "bishop.h"
#include "chess.h"
bishop::bishop(string pos, char color)
{
int x = pos[0] - 97;
int y = pos[1] - 1;
name = "bishop";
this->color = color;
board.add_new(*this);
}
/*chess.cpp*/
#include "chess.h"
int main()
{
...
}
Why are the variables defined twice in here? I thought that first time when chess.h is included, CHESS is defined. So in bishop.cpp, #include "chess.h" will not do anything since the header will skip to #endif from #ifndef CHESS. But it does not work like that obviously. Why am I wrong?
The #ifndef only blocks the code if the symbol is defined within the same translation unit, at a point before the #ifndef. A translation unit is a source file (.cpp) and all the files that are included into it. Since you're compiling two source files, they'll both have a complete include of the .h file.
You already appear to know how to handle the problem of defining global variables in a header file: declare them extern in the header, and put a definition into one of the sources. I would be remiss though if I didn't warn you to avoid globals in the first place, as your programs grow they'll make your life difficult.
I've defined a struct in a header file global.h, that i try to use it in a another class, but i get this error : Error 6 error LNK2001: unresolved external symbol "struct tag_KG_Data g_GlobalVar" (?g_GlobalVar##3Utag_KG_Data##A) KGComThread.obj
#ifndef GLOBAL_H_
#define GLOBAL_H_
#include <stdio.h>
typedef struct tag_KG_Data
{
int nKGStationID;
int nKGComPort;
}GLOBAL_VAR;
#endif
and in KGComThread.cpp file i use it like this:
#include "global.h"
extern GLOBAL_VAR g_GlobalVar;
I think the compiler can't find the global.h file so it defines a meaningless tag_KG_Data struct, but i can't understand why.
This
extern GLOBAL_VAR g_GlobalVar;
is only a declaration. The variable is not yet defined:
GLOBAL_VAR g_GlobalVar;
You need the previous line in a single implementation file.
Also, since this is C++, you don't need a tag for the struct, you can just write
struct GLOBAL_VAR
{
int nKGStationID;
int nKGComPort;
};
I am trying to use a global variable from separated .cpp files. I have got an init.h file as:
//init.h
#ifndef init
#define init
int a = 3;
#endif
I have got an init.cpp file as:
//init.cpp
#include init.h
Then finally my main.cpp file is:
//main.cpp
#include "init.h"
int main(void)
{
while(1)
{
}
}
After this, I get the error:
1>init.obj : error LNK2005: "int a" (?a##3HA) already defined in main.obj
1> ..deneme.exe : fatal error LNK1169: one or more multiply defined symbols found
Why my #infdef control does not solve this problem?. I also tried using #pragma once but I got same error. What is wrong with my code?
You need to mark your variable as extern and define it only once in an implementation file.
As the code is now, you're breaking the one definition rule. The include guards don't help in this case, since all translation units that include that header re-define the variable.
What you actually need:
//init.h
#ifndef init
#define init
extern int a;
#endif
and the definition:
//init.cpp
#include "init.h"
int a = 3;
Also, think twice before using globals. What is it that you're actually trying to achieve?
I recently tried to create a global header file which would have all definitions of error codes (i.e. NO_ERROR, SDL_SCREEN_FLIP_ERROR, etc.) these would just be integers which I would define here.
I included these in both of my .cpp files, however I am getting an error where it is stated that I am defining then twice.
globals.h:
#pragma once
// error related globals
int SCREEN_LOAD_ERROR = 1;
int NO_ERROR = 0;
main.cpp:
#include "globals.h"
#include "cTile.h"
/* rest of the code */
cTile.h:
#pragma once
#include "globals.h"
class cTile {
};
It is complaining that SCREEN_LOAD_ERROR and NO_ERROR are defined twice, but as far as I know #pragma once should prevent this (I also tried #ifndef, but this also did not work).
compiler output:
1>main.obj : error LNK2005: "int SCREEN_LOAD_ERROR" (?SCREEN_LOAD_ERROR##3HA) already defined in cTile.obj
1>main.obj : error LNK2005: "int NO_ERROR" (?NO_ERROR##3HA) already defined in cTile.obj
Am I missing something?
Do not declare variables inside your header file.
When you declare a variable in header file a copy of the variable gets created in each translation unit where you include the header file.
Solution is:
Declare them extern inside one of your header file and define them in exactly one of your cpp file.
globals.h:
extern int SCREEN_LOAD_ERROR;
extern int NO_ERROR;
globals.cpp:
#include "globals.h"
int SCREEN_LOAD_ERROR = 0;
int NO_ERROR = 0;
main.cpp:
#include "globals.h"
cTile.h:
#include "globals.h"
You could simply use an enum:
globals.h:
enum
{
SCREEN_LOAD_ERROR = 1,
NO_ERROR = 0,
// ...
}
using #ifndef works fine.(Although it works, this is not best practice). try like this:
globals.h
#ifndef GLOBALS
#define GLOBALS
int SCREEN_LOAD_ERROR = 1;
int NO_ERROR = 0;
#endif
cTile.h:
#include "globals.h"
class cTile {
};
main.cpp:
#include "globals.h"
#include "cTile.h"
/* rest of the code */
I am using visual studio 2010 and believe I have a project settings issue. I have a header file that has some declarations in it:
definitions.h
#include <string>
struct myStruct
{
std::string x[4];
std::string y[8];
};
void InitializeStructData();
extern myStruct data[12];
and the cpp file initializes my structure:
definitions.cpp
#include "definitions.h"
#include <string>
mySturct data[12];
void InitializeStructData()
{
data[0].x[0] = "a";
data[0].x[1] = "b";
....
data[0].y[0] = "a";
....
....
data[11].y[7] = "done initializing"';
}
and I have a form that has some buttons and things whose text I populate from the arrays depending on different circumstances:
myForm.cpp
#include "definitions.h"
...
//form initialization
As soon as I have two #include "definitions.h" statements I get link errors:
Error 1 error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * Definitions"
Error 2 error LNK1169: one or more multiply defined symbols found
Your question is missing the important part.
You have a std::string* Definitions in a header, that you forgot to use extern with.
Do you have your code (.h file) inside:
#ifndef DEFINITIONS_H
#define DEFINITIONS_H
#endif
to help prevent you from defining it multiple times, if you have it included in multiple places?