Multiple Definitions

M

Mark

I keep getting this multiple definition of scr error. It says it's
defined once in main.cc, and first defined in screen.cc
I'm assuming this is because both screen.cc and main.cc include
screen.h, but I figured the #ifndef would take care of that. If
that's not what it's good for, what IS it good for? Anyways, I'm
clearly doing something wrong, what is the proper way of setting this
up?


// main.cc

#include "screen.h"

int main(int argc, char *argv[])
{

SDL_Quit();
return EXIT_SUCCESS;
}

// screen.cc

#include <SDL_image.h>
#include "screen.h"

extern SDL_Surface *scr;

void screen(int width, int height, const char *title, const char
*icon)
{
SDL_Init(SDL_INIT_VIDEO);
atexit(SDL_Quit);
SDL_WM_SetCaption(title, 0);
SDL_WM_SetIcon(IMG_Load(icon), 0);
scr = SDL_SetVideoMode ( width, height, 32, SDL_HWSURFACE |
SDL_DOUBLEBUF );
}

// screen.h

#ifndef _screen_H
#define _screen_H

#include "SDL.h"

SDL_Surface *scr;

void screen(int width, int height, const char *title="untitled", const
char *icon=NULL);

#endif /* _screen_H */
 
N

Noah Roberts

Mark said:
// screen.cc

#include <SDL_image.h>
#include "screen.h"

extern SDL_Surface *scr;

Change to:
SDL_Surface * scr;

#ifndef _screen_H
#define _screen_H

#include "SDL.h"

SDL_Surface *scr;

Change to:
extern SDL_Surface * scr;
 
M

Mark

Change to:
SDL_Surface * scr;




Change to:
extern SDL_Surface * scr;

heh...that's simple enough. I just figured it was standard to
actually declare the variables in the .h file, since that's what we do
with classes. But this will do just fine, thank you very much!
 
N

Noah Roberts

Mark said:
heh...that's simple enough. I just figured it was standard to
actually declare the variables in the .h file, since that's what we do
with classes. But this will do just fine, thank you very much!

You do.

extern SDL_Surface * scr;

is a "declaration" of a variable.

SDL_Surface * scr;

is a "definition" of a variable.

As with classes and other things, you can declare many times...you can
only define once. So you only *define* in a .cpp file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top