Jump to content
  • Advertisement
Sign in to follow this  
Zeophlite

[Linker error] undefined reference to

This topic is 4965 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
Advertisement
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
It may sound stupid but, is the object file linked in?
Can you reproduce the error in an independent code example?

Share this post


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


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!