C++ Default Arguments - Declaration

10,234

Solution 1

You should not specify the default argument both in the function declaration and in the function definition. I suggest you putting it in the declaration only. For instance:

class FileReader{
public:
    FileReader(const char*);                        
    std::string trim(std::string string_to_trim, const char trim_char = '=');
    //                                                                ^^^^^
    //                                                     Here you have it
};

std::string FileReader::trim(std::string string_to_trim, const char trim_char)
//                                                                  ^^^^^^^^^
//                                              So here you shouldn't have it
{
    // ....
}

In case both the function definition and the function declaration are visible to the compiler from the point where the function call is being made, you would also have the option of specifying the default arguments in the function definition only, and that would work as well.

However, if only the declaration of the function is visible to the compiler, then you will have to specify the default argument in the function declaration only, and remove them from the function definition.

Solution 2

inside the cpp you don't need the default parameter , only in the h file

Share:
10,234
Normal People Scare Me
Author by

Normal People Scare Me

I'm interested in Python, Perl, C, C++, LISP, PHP and Fortran.

Updated on June 04, 2022

Comments

  • Normal People Scare Me
    Normal People Scare Me about 2 years

    I created a function in my class. I put all my declarations in my header file and all my definitions in my .cpp .

    In my header :

    class FileReader{
     
    public:
    FileReader(const char*);                        //Constructor
    std::string trim(std::string string_to_trim, const char trim_char = '=');
    
    };
    

    In my .cpp :

    std::string FileReader::trim(std::string string_to_trim, const char trim_char = '='){
    
    std::string _return;
    for(unsigned int i = 0;i < string_to_trim.length();i++){
        if(string_to_trim[i] == trim_char)
            continue;
        else
            _return += string_to_trim[i];
    }
    
           return _return;
    }
    

    Whenever I try to compile and run it, I get two errors.

    error: default argument given for parameter 2 of 'std::string FileReader::trim(std::string, char)' [-fpermissive]

    error: after previous specification in 'std::string FileReader::trim(std::string, char)' [-fpermissive]

    What am I doing wrong? I just want my function to have this default argument.

  • Normal People Scare Me
    Normal People Scare Me over 11 years
    Well Thanks, worked. I removed the default argument in my definition.
  • Andy Prowl
    Andy Prowl over 11 years
    @CGuy: That's strange. What compiler are you using? It works here.
  • Normal People Scare Me
    Normal People Scare Me over 11 years
    @AndyProwl the gnu c++ compiler
  • Andy Prowl
    Andy Prowl over 11 years
    @CGuy: See the update to my answer. If you are invoking the function from a point where only the declaration is visible, you will have to put the default argument in the declaration only.