C++ Class Linker error

Started by
13 comments, last by MJP 16 years ago
The problem is with this code you posted:

void Block::drawSprite(SDL_Surface *string, SDL_Surface *screen, int srcx, int srcy, int srcw, int srch, double destx, double desty, int destw, int desth);


This is a function declaration (since it has a semi-colon), but you've included the scope as if it were a function definition.
Advertisement
Eh... what? I don't have any scaope there, the braces are normal braces not curly ones, it just represents the parameters of the function doesn't it?
Ok, you seem to be confused about some terms:

1. Class member function declaration:

this is the line that tells your compiler what your funcion expects as parameters and returns as results. In your case:
class Block{public:double destx;double desty;int destw;int desth;int setblock;int pos;bool move;//Function call to draw a sprite.void drawSprite(SDL_Surface *string, SDL_Surface *screen, int srcx, int srcy, int srcw, int srch, double destx, double desty, int destw, int desth); // THE FUNCTION DECLARATION};

This function declaration declares the function in the Block scope, since the declaration is inside the scope of the Block class declaration.

2. Class member function definition:

This part describes what the previously declared function actually does. In your case:

void Block::drawSprite(SDL_Surface *string, SDL_Surface *screen, int srcx, int srcy, int srcw, int srch, double destx, double desty, int destw, int desth){// actual code goes here}


Notice the Block:: scope specifier. Since we're outside the Block class declaration now, we aren't in the Block scope anymore. So we have to specify that the function we're trying to define is in the Block scope explicitly.

EDIT: code formatting
Yea, but the code for the function is actually in a seperate header file, all I'm trying to do here is forward declare to it.
Quote:Original post by DestX
Yea, but the code for the function is actually in a seperate header file, all I'm trying to do here is forward declare to it.


-In general, function implementations (the actual function code) shouldn't go in header files. Do you mean .cpp file? Or are you talking about the function declaration?

-I'm not sure what you mean by "forward declare to it". Once a member function is declared in the class and the implementation is defined, there's no need to "forward declare" anything. You simply #include the header file containing the class definition in any .cpp files that use the class.

This topic is closed to new replies.

Advertisement