Extern Problems

Could someone tell me what I'm not seeing here? I'm getting undefined references to DOT_WIDTH, DOT_HEIGHT, and SCREEN_WIDTH.

Here are the two main source files and the header:

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
//The headers
#include "DotNTimer.h"
#include "SDL/SDL_image.h"
#include <string>

//The screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The dimensions of the dot
const int DOT_WIDTH = 20;
const int DOT_HEIGHT = 20;

//The frame rate
const int FRAMES_PER_SECOND = 20;

//The surface
SDL_Surface *dot = NULL;
SDL_Surface *screen = NULL;

//The event structure
SDL_Event event;

// ...

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    //Holds offsets
    SDL_Rect offset;

    //Get offsets
    offset.x = x;
    offset.y = y;

    //Blit
    SDL_BlitSurface( source, clip, destination, &offset );
}

// ...

}


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
#include "DotNTimer.h"

extern const int SCREEN_WIDTH;
extern const int SCREEN_HEIGHT;
extern const int SCREEN_BPP;

extern const int DOT_WIDTH;
extern const int DOT_HEIGHT;

extern SDL_Surface * dot;
extern SDL_Surface * screen;

extern SDL_Event event;
extern void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL);

Dot::Dot()
{
	// Initialize the offsets
	x = 0;
	y = 0;
	
	// Initialize the velocity
	xVel = 0;
	yVel = 0;
}

void Dot::handle_input()
{
	// If a key was pressed
	if (event.type == SDL_KEYDOWN)
	{
		// Adjust the velocity
		switch (event.key.keysym.sym)
		{
			case SDLK_UP:		yVel -= DOT_HEIGHT / 2; break;
			case SDLK_DOWN:		yVel += DOT_HEIGHT / 2; break;
			case SDLK_LEFT:		xVel -= DOT_WIDTH / 2; break;
			case SDLK_RIGHT:	xVel += DOT_WIDTH / 2; break;
		}
	}
	// If a key was released
	else if (event.type == SDL_KEYUP)
	{
		// Adjust the velocity
		switch(event.key.keysym.sym)
		{
			case SDLK_UP:		yVel += DOT_HEIGHT / 2; break;
			case SDLK_DOWN:		yVel -= DOT_HEIGHT / 2; break;
			case SDLK_LEFT:		xVel += DOT_WIDTH / 2; break;
			case SDLK_RIGHT:	xVel -= DOT_WIDTH / 2; break;
		}
	}
}

// ...

void Dot::show()
{
	// Show the dot
	apply_surface(x, y, dot, screen);
}


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
#ifndef _DotNTimer_H_
#define _DotNTimer_H_

#include "SDL/SDL.h"

// The timer
class Timer
{
private:
	// The clock time when the timer started
	int startTicks;
	
	// The ticks stored when the timer was paused
	int pausedTicks;
	
	// The timer status
	bool paused;
	bool started;
public:
	Timer();
	void start();
	void stop();
	void pause();
	void unpause();
	int get_ticks();
	bool is_started();
	bool is_paused();
};

// The dot that will move around on the screen
class Dot
{
private:
	// The X and Y offsets of the dot
	int x, y;
	
	// The velocity of the dot
	int xVel, yVel;
public:
	// Initializes the variables
	Dot();
	
	// Takes key presses and adjusts the dot's velocity
	void handle_input();
	
	// Moves the dot
	void move();
	
	// Shows the dot on the screen
	void show();
};

#endif 
Thank you very much for the resource. Once I added extern both it worked but i'm a little bit confused. The link said:
"a const-qualified object at file scope" but this isn't an object. Now what I'm also confused about is what does that make the initialized variable? a definition or a declaration? Is this a keyword used for situations like this or is there something more?
Objects with the const specifier have internal linkage. So these definitions

//The screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The dimensions of the dot
const int DOT_WIDTH = 20;
const int DOT_HEIGHT = 20;


are visible only in translation units that contain this header.
There is no need to define them as external. You can simply include a header where they are defined in each translation unit. Or you can declare them as external but define them only in one module.
Last edited on
Nevermind I think i got it. Seems I misunderstood the concept of extern which merely makes the variable of external linkage but doesn't determine whether a variable is a definition or declaration. Thank you once again ne555.
if a variable with specifier extern has no an initializer then it is declared (not defined). If a variable with specifier extern has an initializer then it is defined.
Topic archived. No new replies allowed.