• 13
• 18
• 19
• 27
• 10

# [Linker error] undefined reference to

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

What does the above compile error mean?
class CSprite {
public:
void init(CSpriteBase *base, SDL_Surface *screen);
};

class CUnit : public CSprite {
CUnit(CSpriteBase & base, SDL_Surface* screen);
};

CUnit :: CUnit(CSpriteBase & base, SDL_Surface* screen) {
init(&base,screen);
}

CSprite cannot change and CSprite::init() doesn't change CUnit inherits CSprite, and its constructor has to call CSprite::init() I get this error: [Linker error] undefined reference to CSprite::init(CSpriteBase*, SDL_Surface*)' [Edited by - Fruny on December 16, 2004 8:06:28 PM]

##### Share on other sites
You will have to define the method somewhere, e.g. have a CPP file containing
void CSprite::init(CSpriteBase *base, SDL_Surface *screen) {}

This will fix the error. Declaring a function will not be sufficient; if you call it somewhere else in the code it mustbe defined as well.

##### Share on other sites
It is defined later in the code, I was thinking that the pointers and addresses were incorrect?

##### Share on other sites
Whatever the reason, the linker is saying that the function CSprite::init(CSpriteBase*, SDL_Surface*) is referenced somewhere but is not implemented (aka defined).

The obvious cause of this error is forgetting to define the function (as darookie suggested). Another cause is a typo in the parameters, but since your function is declared in a class, that mistake would usually generate a compiler error. Another common cause is forgetting to prepend the scope to the function's name in the definition. Did you define the function like this?
void CSprite::init(CSpriteBase *base, SDL_Surface *screen){    ...} `

##### Share on other sites
Another thing to check for is to make sure that you didn't put the init function definition in a cpp file while also making it inline. This will make it so you can only call the function from the same translation unit as the definition.

##### Share on other sites
Direct from my project:

CSprite.h:
class CSprite {
public:
void init(CSpriteBase *base, SDL_Surface *screen);
...
}

CSprite.cpp:
void CSprite::init(CSpriteBase *base, SDL_Surface *screen) {
...
}

##### Share on other sites
It may sound stupid but, is the object file linked in?
Can you reproduce the error in an independent code example?

##### Share on other sites
I don't understand what you mean by the object file being linked in?
I'm using Dev-C++, so doesn't that take care of the makefile?

##### Share on other sites
Ah that fixed it.

I forgot to add those files to my project, thanks Fruny

##### Share on other sites
Is "CSprite.h" #included in the CUnit implementation?