New Reply

[C++] g++ compiler issues

 
Share Thread Thread Tools
Jul22-12, 11:11 PM   #1
 

[C++] g++ compiler issues


I started using g++ and the command line on linux.

In one section of my code, I provide a default argument for a function parameter.
I'm just wondering why this sometimes brings up an error if i don't include -fpermissive
when I'm compiling everything.


As an example. This is the apply_surface function (taken from lazyfoo.net SDL tutorials):
Code:
 void apply_surface (int x, int y, SDL_Surface * source, SDL_Surface * destination,
  SDL_Rect * clip = NULL) {
    SDL_Rect offsets;
    offsets.x = x;
    offsets.y = y;

    SDL_BlitSurface (source, clip, destination, &offsets);
}
Even if i were to enter:
Code:
apply_surface (0, 0, backGround, screen);
or
Code:
apply_surface (0, 0, backGround, screen, NULL);
These will result in the following error:
main.cpp:81:103: error: default argument given for parameter 5 of ‘void applySurface(int, int, SDL_Surface*, SDL_Surface*, SDL_Rect*)’ [-fpermissive]
main.cpp:16:6: error: after previous specification in ‘void applySurface(int, int, SDL_Surface*, SDL_Surface*, SDL_Rect*)’ [-fpermissive]

But if I were to use this function (also with a default argument):
Code:
void function (int x, int y = 0) {
    std::cout << x << "\t" << y << std::endl;
}
Then it'll compile perfectly, without any errors.

So is this something that I will just have to deal with all the time, or is there some setting i can
change to make it not necessary to include -fpermissive when the error pops up (other than make files)?

Any help is appreciated, and I hope that i have been detailed enough in the explanation of my problem.

-Peter P.
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Galaxies fed by funnels of fuel
>> The better to see you with: Scientists build record-setting metamaterial flat lens
>> Google eyes emerging markets networks
Jul23-12, 01:37 AM   #2
 
Recognitions:
Homework Helper Homework Help
Hi Peter P!

You should not get those errors.
The fact that you do implies there is something wrong with your code (that is not apparent from what you show).

Did you perhaps specify
Code:
void apply_surface (int x, int y, SDL_Surface * source, SDL_Surface * destination,
  SDL_Rect * clip = NULL)
twice?
Once in the definition and once in a prototype declaration?

Normally you'd have a prototype declaration in a .h file with "= NULL", and you'd have the definition in a .cpp file without "= NULL".
Jul23-12, 08:02 AM   #3
 
Agree, most likely double declaration.

Unrelated, but do have define guards on your header files?
Jul23-12, 11:20 AM   #4
 

[C++] g++ compiler issues


Ah, thanks for the help. It was the double declaration problem. I never know that you only had to specify default parameters in function prototypes and not the actual definition.
Jul23-12, 02:16 PM   #5
 
I don't know how people remember all the words like declarations and definitions; I was never that strong on it, and using dynamic-typed languages has blurred the line for me.
New Reply
Thread Tools


Similar Threads for: [C++] g++ compiler issues
Thread Forum Replies
compiler help Programming & Comp Sci 1
Does anyone here use the compiler for PIC processors from CCS? Electrical Engineering 1
Compiler Computing & Technology 1
C++ Compiler? Computing & Technology 8
Compiler Computing & Technology 7