default arguments in classes

I am not sure why i cannot figure this out. In a simplified script, it runs fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

class Test{
    public:
        int a;
        void func(int arg);
};

void Test::func(int arg=0){
    a = arg;
}

int main(){
    Test obj;
    obj.func();
    std::cout << obj.a << std::endl;
}


However in my file i am working on, i get an error regarding the default argument:

texture_manager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <string>

class TextureManager{
    public:
        TextureManager();
        bool load(std::string filename, std::string id, SDL_Renderer* renderer);
        void draw(std::string id, int x, int y, int width, int height, 
            SDL_Renderer* renderer, SDL_RendererFlip flip=SDL_FLIP_NONE);
        void draw_frame(std::string id, int x, int y, int width, int height, 
            int current_row, int current_frame, SDL_Renderer* renderer, SDL_RendererFlip flip);
};


texture_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "texture_manager.h"

TextureManager::TextureManager(){
    
}

void TextureManager::draw(std::string id, int x, int y, int width, int height, 
    SDL_Renderer* renderer, SDL_RendererFlip flip=SDL_FLIP_NONE){
        
}
void TextureManager::draw_frame(std::string id, int x, int y, int width, int height, 
    int current_row, int current_frame, SDL_Renderer* renderer, SDL_RendererFlip flip=SDL_FLIP_NONE){
        
}


compile/error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
metulburr@arch ~/repos/sdl/test $ make
g++ -c -I/usr/include/SDL2 -std=c++11 -Wall main.cpp -o main.o -lSDL2 -lSDL2_image
g++ -c -I/usr/include/SDL2 -std=c++11 -Wall Control.cpp -o Control.o -lSDL2 -lSDL2_image
g++ -c -I/usr/include/SDL2 -std=c++11 -Wall Character.cpp -o Character.o -lSDL2 -lSDL2_image
g++ -c -I/usr/include/SDL2 -std=c++11 -Wall texture_manager.cpp -o texture_manager.o -lSDL2 -lSDL2_image
texture_manager.cpp:10:64: error: default argument given for parameter 7 of 'void TextureManager::draw(std::string, int, int, int, int, SDL_Renderer*, SDL_RendererFlip)' [-fpermissive]
     SDL_Renderer* renderer, SDL_RendererFlip flip=SDL_FLIP_NONE){
                                                                ^
In file included from texture_manager.cpp:3:0:
texture_manager.h:11:14: error: after previous specification in 'void TextureManager::draw(std::string, int, int, int, int, SDL_Renderer*, SDL_RendererFlip)' [-fpermissive]
         void draw(std::string id, int x, int y, int width, int height, 
              ^
makefile:16: recipe for target 'texture_manager.o' failed
make: *** [texture_manager.o] Error 1
metulburr@arch ~/repos/sdl/test $ 

The difference between your simplified example at the top and your actual code is that in the simplified example the default parameter is only given in the .cpp where you implement the function. In your actual code, you are definining a default argument in both the .h and .cpp.
oh my fault...i was only paying attention to draw_frame and completely disregarded draw's default argument. That was a complete brain fart right there.

thanks.
Last edited on
Topic archived. No new replies allowed.