Sign in to follow this  

[Linker error] undefined reference to

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this