Why Does g++ Require -fpermissive for Default Arguments in Some Cases?

  • Context: C/C++ 
  • Thread starter Thread starter Peter P.
  • Start date Start date
  • Tags Tags
    Compiler Issues
Click For Summary

Discussion Overview

The discussion revolves around the behavior of the g++ compiler regarding default arguments in function declarations and definitions. Participants explore why the inclusion of the -fpermissive flag is sometimes necessary when compiling code that uses default arguments, particularly in the context of a specific example involving the SDL library.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Peter P. describes encountering errors when compiling code with default arguments unless -fpermissive is used, questioning whether this is a common issue or if there is a setting to avoid it.
  • One participant suggests that the errors indicate a potential issue in the code, specifically the possibility of a double declaration of the function with default parameters specified in both the prototype and the definition.
  • Another participant agrees with the assessment of a likely double declaration and inquires about the use of define guards in header files.
  • Peter P. later confirms that the issue was indeed due to the double declaration and expresses uncertainty about the distinction between declarations and definitions.

Areas of Agreement / Disagreement

Participants generally agree that the errors are likely due to a double declaration of the function. There is no disagreement on this point, but the discussion does not resolve broader questions about compiler behavior or settings beyond the specific case presented.

Contextual Notes

The discussion highlights the importance of understanding function declarations and definitions in C++, particularly regarding default arguments. It also reflects on the potential confusion that can arise from transitioning between dynamically typed languages and statically typed languages like C++.

Who May Find This Useful

This discussion may be useful for programmers using g++ who are encountering issues with default arguments, as well as those looking to clarify the distinctions between function declarations and definitions in C++. It may also benefit individuals transitioning from dynamic to static typing in programming languages.

Peter P.
Messages
23
Reaction score
0
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.
 
Technology news on Phys.org
Hi Peter P! :smile:

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".
 
Agree, most likely double declaration.

Unrelated, but do have define guards on your header files?
 
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.
 
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.
 

Similar threads

  • · Replies 39 ·
2
Replies
39
Views
5K
Replies
4
Views
11K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
6
Views
4K
Replies
11
Views
2K