Linker Error?!

Started by
2 comments, last by Joshnathan 19 years, 4 months ago
Hi all, I got this very weird problem. This is my source file:

#include "world.h"
#include <SDL/SDL_image.h>

#define WORLD_W 40
#define WORLD_H 30

     
bool InitWorld()
{
     SDL_Surface *temp = IMG_Load("map.gif");
     
     for(int j = 0; j < WORLD_H; j++)
     {
             for(int i = 0; i < WORLD_W; i++)
             {
                     world[j].image = temp;
                     world[j].value = 0;
                     world[j].x = i * 20;
                     world[j].y = j * 20;
             }
     }
     return true;
}

void DisplayWorld(SDL_Surface *screen)
{
     
     for(int j = 0; j < WORLD_H; j++)
     {
             for(int i = 0; i < WORLD_W; i++)
             {
                     SDL_Rect rect;
                     rect.x = world[j].x;
                     rect.y = world[j].y;
                     SDL_BlitSurface(world[j].image, NULL, screen, &rect);
             }
     }
}

void Water()
{
     SDL_Surface *temp = IMG_Load("water.gif");
     
     for(int j = 20; j < 25; j++)
     {
             for(int i = 15; i < 25; i++)
             {
                     world[j].image = temp;
                     world[j].value = 1;
             }
     }
}

void EraseTrace(int x, int y, SDL_Surface *screen)
{
     SDL_Surface *erase = IMG_Load("map.gif");
     SDL_Rect rect;
     rect.x = x;
     rect.y = y;
     SDL_BlitSurface(erase, NULL, screen, &rect);
}

and these are my errors: [Linker error] undefined reference to `world' (x10 or more) world.cpp C:\WINDOWS\Desktop\Joshua\RPG\world.o(.text+0x13d) more undefined references to `world' follow Anyone know what is causing these problems?
-----------------------------Sismondi GamesStarted c++ in October 2004...
Advertisement
Assuming you're still working on the same thing as your last thread, it sounds like you declared a global variable extern in your header without actually defining it anywhere.

If you declare a global extern in a header file, in one source file you need to define the variable. So if you have extern WORLD world[40][30]; in your header, in one source file put WORLD world[40][30]; at namespace scope.
Where is the definition for world?

I assume you have an extern whatever world; in world.h, otherwise the code would not compile, but that's only a declaration.

See [orgfiles] for a potential explanation.

In the meantime, post your world.h file.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
thank you so much! :D
I really don't know what I would do without gamedev lol.
I can even see my hero move and bump into some areas!
-----------------------------Sismondi GamesStarted c++ in October 2004...

This topic is closed to new replies.

Advertisement