Problem with files and global variables

May 12, 2013 at 8:40am
I have a problem and I can't understand any of the solutions online. It's a file management problem.

I have 5 files:
- Sprite.cpp, Sprite.h
- Game.cpp, Game.h
- Main.cpp

Sprite and Game cpps #include their header counterpart.

I have class Sprite defined in Sprite.cpp

I want Game to store all the global instances in my program, such as arrays of Players, Enemies and Sprites which are accessable to every file in my program.

The problem is Sprite becomes dependant on Game to access these global variables. But Game
is dependant on Sprite to create the array of Sprites. This causes problems when they try to import each others header files.

The exact code is this (each header has the #ifndef NAME_INCLUDED thing):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//sprite.h:
#include "game.h"
class Sprite
{
public:
   Sprite();
};

//sprite.cpp:
#include "sprite.h"
Sprite::Sprite(){}

//game.h:
#include "sprite.h"

extern Sprite sprite;

//game.cpp:
#include "game.h"

Sprite sprite;

//main.cpp:
#include "game.h"

int main()
{
   return 0;
}


The error message I get:
Error 1 error C2146: syntax error : missing ';' before identifier 'sprite'
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Is their a solution to this problem or can you suggest alternative ways of organising data that needs to be accessed by many classes?

Thanks
May 12, 2013 at 8:50am
You don't need the full class definition to do extern declaration. Instead of including sprite.h you just need to tell that Sprite is a class.
1
2
3
4
5
6
#include "sprite.h"
class Sprite;
extern Sprite sprite;
// or
#include "sprite.h"
extern class Sprite sprite;


It is also not clear from the code that you have posted why sprite.h needs to include game.h.
Last edited on May 12, 2013 at 8:59am
May 12, 2013 at 9:30am
I've expanded the example program a little to show you why sprite.h needs to include game.h for my example. Also I implemented the class Sprite; reference in game.h and now I get new errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//sprite.h
#ifndef SPRITE_H_INCLUDED
#define SPRITE_H_INCLUDED

#include "game.h"

class Sprite
{
private:
	int x;
	int y;
public:
	Sprite();
	Sprite(int x, int y);
	~Sprite();
	int afunct();
	void moveLeft();
};

#endif

//sprite.cpp
#include "sprite.h"

Sprite::Sprite()
{
}

Sprite::Sprite(int x, int y)
{
	this->x = x;
	this->y = y;
}

Sprite::~Sprite()
{

}

int Sprite::afunct()
{
	return 1;
}

void Sprite::moveLeft()
{
	if(grid[x][y-1] == 0)
	{
		grid[x][y] = 0;
		y--;
		grid[x][y] = 1;
	}
}

//game.h
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

class Sprite;
extern Sprite spriteList[1000];

extern int grid[10][10];

void iniGrid();

#endif

//game.cpp
#include "game.h"

int grid[10][10];

void iniGrid()
{
	for(int i=0;i<10;i++)
	{
		for(int j=0;j<10;j++)
		{
			grid[i][j]=0;
		}
	}
}

//main.h
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED

#include "game.h"
#include "sprite.h"
#include <string>
#include <iostream>
#include "conio.h"

#endif

//main.cpp
#include "main.h"


using namespace std;

int main()
{
	Sprite sprite(5,5);
	iniGrid();
	sprite.moveLeft();
	cout << sprite.afunct();
	//spriteList[0] = sprite;
	//spriteList[0].afunct();

	_getch();
	return 0;
}


Compiling the above works and prints "1" as expected. But when I uncomment the spriteList lines that are in reference to the extern spriteList made in game.h I get this error:

Error 2 error LNK1120: 1 unresolved externals
Error 1 error LNK2001: unresolved external symbol "class Sprite * spriteList" (?spriteList@@3PAVSprite@@A)
May 12, 2013 at 10:02am
That's because you have not defined spriteList anywhere. Put Sprite spriteList[1000]; in game.cpp.

sprite.h still doesn't need to include game.h but if you remove the include you will have to include game.h in sprite.cpp instead.
May 12, 2013 at 11:32am
Adding Sprite spriteList[1000]; gives me this error now:

Error 1 error C2148: total size of array must not exceed 0x7fffffff bytes
Error 2 error C2512: 'Sprite' : no appropriate default constructor available
Topic archived. No new replies allowed.