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

In summary, the conversation discusses the use of default arguments in function parameters and the errors that can arise if they are not properly specified. It also touches on the difference between function prototypes and definitions and the importance of defining variables only once.
  • #1
Peter P.
23
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
  • #2
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".
 
  • #3
Agree, most likely double declaration.

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

1. Why is my code not compiling with the g++ compiler?

The most common reason for code not compiling with the g++ compiler is typically due to syntax errors or missing headers. Make sure to carefully check your code for any typos or missing semicolons. Also, ensure that all necessary headers are included for any libraries or functions being used.

2. How do I specify the version of the g++ compiler to use?

You can specify the version of the g++ compiler to use by using the -std flag followed by the version number. For example, to use C++17, you would use the flag -std=c++17.

3. Why am I getting linker errors with the g++ compiler?

Linker errors can occur when the compiler is unable to find the definitions for certain functions or variables used in the code. Make sure all necessary libraries are linked and check for any missing or mismatched function prototypes.

4. How can I optimize my code with the g++ compiler?

To optimize your code with the g++ compiler, you can use the -O flag followed by a number from 1 to 3, with 3 being the highest level of optimization. Keep in mind that higher levels of optimization may affect the readability and maintainability of your code.

5. How do I enable/disable warnings with the g++ compiler?

You can enable or disable specific warnings with the g++ compiler by using the -W flag followed by the name of the warning to enable or disable. For example, to enable the unused variable warning, you would use -Wunused. To disable a warning, use -Wno-[warning name].

Similar threads

  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
10K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
13
Views
19K
  • Programming and Computer Science
2
Replies
49
Views
10K
  • Programming and Computer Science
Replies
17
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
Back
Top