Hey, I am having trouble trying to set a default value for a function in one of my classes.
Here is where I am trying to set it:

void Sprite::Spr_Blit(SDL_Surface* source, SDL_Surface* dest, SDL_Rect* clip = NULL)
{
    SDL_Rect offset;
    offset.x = getX();
    offset.y =getY();

    SDL_BlitSurface(source,clip,dest,&offset);
}

Here is the Spr_Blit defined in my header:

void Spr_Blit(SDL_Surface* source, SDL_Surface* dest, SDL_Rect* clip);

And here is where I call this function in main:

//refresh the bg
        g_level.Spr_Blit(g_level.getSprite(),g_SrWindow);
        //draw player
      //  g_player.Spr_Blit(g_player.getSprite(),g_SrWindow);

This is the error I receive:

main.cpp||In function ‘int main(int, char**)’:|
main.cpp
|163|error: no matching function for call to ‘Sprite::Spr_Blit(SDL_Surface*, SDL_Surface*&)’|
Sprite.h
|15|note: candidates are: void Sprite::Spr_Blit(SDL_Surface*, SDL_Surface*, SDL_Rect*)|
main.cpp
|165|error: no matching function for call to ‘Player::Spr_Blit(SDL_Surface*, SDL_Surface*&)’|
Sprite.h
|15|note: candidates are: void Sprite::Spr_Blit(SDL_Surface*, SDL_Surface*, SDL_Rect*)|
||=== Build finished: 2 errors, 0 warnings ===|


It looks like it is trying to combine the last 2 arguments or something? Can anyone give me a push?

Your default argument should be in the function's declaration.

It is though? Or do you mean the function prototype?

I should've said declaration/prototype.

In the definition of Spr_Blit, you've specified the default. Remove it from there and put it in the prototype instead.

Also, it looks like you're passing functions as arguments to another function. There's a particular type of syntax that's involved in doing that.

int foo (int r) {
return r * r;
}

int main()
{
int (*pfoo)(int); // ptr to a function that accepts an int and returns an int
pfoo = foo;      // pfoo now points to the function foo
return 0;
}

The terms declaration and prototype are generally considered synonymous. You have your default value in the function's definition/implementation. Sometimes the definition qualifies as the declaration/prototype, but it's not a given; it depends on how you write your code.

The terms declaration and prototype are generally considered synonymous. You have your default value in the function's definition/implementation. Sometimes the definition is the declaration/prototype, but it's not a given; it depends on how you write your code.

Ye sorry, Ok, I changed my prototype to this:

void Spr_Blit(SDL_Surface* source,SDL_Surface* dest,SDL_Rect* clip = NULL);

and my definition to this:

void Sprite::Spr_Blit(SDL_Surface* source,SDL_Surface* dest)
{
    SDL_Rect offset;
    offset.x = getX();
    offset.y =getY();

    SDL_BlitSurface(source,clip,dest,&offset);
}

and got this error:
Sprite.cpp|52|error: prototype for ‘void Sprite::Spr_Blit(SDL_Surface*, SDL_Surface*)’ does not match any in class ‘Sprite’|

How many overloads of Spr_Blit do you have? If you have Spr_Blit(SDL_Surface*, SDL_Surface*) and Spr_Blit(SDL_Surface*, SDL_Surface*, SDL_Rect*=NULL) , you're introducing an ambiguity:

Spr_Blit(surface1, surface2); // Which overload to use?

Let's confirm everything you're trying to do rather than chase errors when there might be a more fundamental issue going on.

void Sprite::Spr_Blit(SDL_Surface* source,SDL_Surface* dest)
{
    SDL_Rect offset;
    offset.x = getX();
    offset.y =getY();

    SDL_BlitSurface(source,clip,dest,&offset);
}

I only see 2 parameters here. Your declaration has 3...

All that you had to do was move the part that sets the default (the "=NULL"), not remove the whole thing.

Declaration:

void Spr_Blit(SDL_Surface* source,SDL_Surface* dest,SDL_Rect* clip = NULL);

Definition:

void Sprite::Spr_Blit(SDL_Surface* source,SDL_Surface* dest)

Obviously you're missing something.

Declaration:

void Spr_Blit(SDL_Surface* source,SDL_Surface* dest,SDL_Rect* clip = NULL);

Definition:

void Sprite::Spr_Blit(SDL_Surface* source,SDL_Surface* dest)

Obviously you're missing something.

Terribly unhelpful post.

Anyway, That was the problem Fbody. When Chilton said remove the default I thought he meant the actual variable. The annoying thing is I could have sworn I tried every variation I of declarations I could think of, I was pretty sure I also tried this! Thanks for the help.

EDIT: I still get the same first error

/home/andy/Desktop/College/Programming/DATA_STRUCTURE_CA/2DGame/AkillingBeck_CA3/main.cpp||In function ‘int main(int, char**)’:|
/home/andy/Desktop/College/Programming/DATA_STRUCTURE_CA/2DGame/AkillingBeck_CA3/main.cpp|165|error: no matching function for call to ‘Player::Spr_Blit(SDL_Surface*, SDL_Surface*&, SDL_Rect)’|
/home/andy/Desktop/College/Programming/DATA_STRUCTURE_CA/2DGame/AkillingBeck_CA3/Sprite.h|15|note: candidates are: void Sprite::Spr_Blit(SDL_Surface*, SDL_Surface*, SDL_Rect*)|
||=== Build finished: 1 errors, 0 warnings ===|

EDIT2:The error only occurs when I try to use all 3 arguments.

Double-check Line 165 of your main.cpp file. It looks like you may have an extraneous operator on the second argument of your call to Player::Spr_Blit(). It thinks you're trying to send it a reference to a pointer to a SDL_Surface. Is that your intent? Also note that it thinks your calling a version of it defined in Player instead of in Sprite. Have you resolved the scope of the function correctly in your call?

FYI:
If you use the [noparse] bbCode around your errors you won't get the random smilies.

This is the line where the error occurs, it only occurs when I enter all 3 arguments:

g_player.Spr_Blit(g_player.getSprite(),g_SrWindow,g_player.getBounds());

getSprite() returns the player surface, g_SrWindow is the window, getBounds returns a rectangle representing the players x,y,w,h.

EDIT: geBounds returns a SDL_Rect, not an SDL_Rect*. Solved.

I apologize if I wasn't explicit enough in telling you that.

Understanding is easy, Teaching is hard.

Ummmh...


*sigh*

your best bet would be to put the defualt value in the definition of the funtion in the header

your best bet would be to put the defualt value in the definition of the funtion in the header

You may want to check your vocabulary. We've already had this discussion. Default values must always be put in the first occurrence of a function's name, and must always be at the end of an argument list. Once you apply a default value to an argument/parameter, all other arguments/parameters after it must also have default values. Putting the default value in the definition was part of the problem because it wasn't the first occurrence of the function's name. That's why so many of the OP's errors disappeared just after that change.

Also, there are very few situations that warrant putting the definition/implementation of a function in a header file. Headers should only have declarations/prototypes in them. Executable code (definitions/implementations) should be in a *.cpp file.

You may want to check your vocabulary. We've already had this discussion. Default values must always be put in the first occurrence of a function's name, and must always be at the end of an argument list. Once you apply a default value to an argument/parameter, all other arguments/parameters after it must also have default values. Putting the default value in the definition was part of the problem because it wasn't the first occurrence of the function's name. That's why so many of the OP's errors disappeared just after that change.

Also, there are very few situations that warrant putting the definition/implementation of a function in a header file. Headers should only have declarations/prototypes in them. Executable code (definitions/implementations) should be in a *.cpp file.

i am very sory i ment to say to put the defualt in the decleration , my bad

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.