I'm having trouble with this piece of code , after i took this class from the main.cpp file and splitted it in to .h and .cpp the compiler started complaining about the default parameters i was using in a void.
/* PBASE.H */ class pBase : public sf::Thread { private: bool Running; public: sf::Mutex Mutex; WORD OriginalColor; pBase(){ Launch(); Running = true; OriginalColor = 0x7; } void progressBar(int , int); bool key_pressed(); void setColor( int ); void setTitle( LPCWSTR ); bool test_connection(){ if(Running == false){ return 0; } else{ return 1; } return 0; } void Stop(){ Running = false; if(Running == false) Wait(); } };
/* PBASE.CPP */ // ... other stuff above void pBase::setColor( int _color = -1){ if(_color == -1){ SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ),FOREGROUND_INTENSITY | OriginalColor); return; } SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ),FOREGROUND_INTENSITY | _color); }
And the error , taken from VC2010
Error 4 error C2572: 'pBase::setColor' : redefinition of default parameter : parameter 1
In JavaScript, function parameters default to undefined . However, it's often useful to set a different default value. This is where default parameters can help. In the past, the general strategy for setting defaults was to test parameter values in the function body and assign a value if they are undefined .
Default arguments are overwritten when the calling function provides values for them. For example, calling the function sum(10, 15, 25, 30) overwrites the values of z and w to 25 and 30 respectively.
No, Standard C does not support either.
Short answer: No. Fortunately, you can simulate them. Many programming languages like C++ or modern JavaScript have a simple option to call a function without providing values for its arguments.
You have to specify the default values for the arguments only in the declaration but not in the definition.
class pBase : public sf::Thread { // .... void setColor( int _color = -1 ); // .... } ; void pBase:: setColor( int _color ) { // .... }
The default value for an member function's argument can either go in declaration or definition but not both. Quote from ISO/IEC 14882:2003(E) 8.3.6
6) Except for member functions of class templates, the default arguments in a member function definition that appears outside of the class definition are added to the set of default arguments provided by the member function declaration in the class definition. Default arguments for a member function of a class template shall be specified on the initial declaration of the member function within the class template. [Example:
class C { void f(int i = 3); void g(int i, int j = 99); }; void C::f(int i = 3) // error: default argument already { } // specified in class scope void C::g(int i = 88, int j) // in this translation unit, { } // C::g can be called with no argument
—end example]
According to the standard provided example, it should actually work the way you did. Unless you have done like this, you shouldn't actually get the error. I amn't sure why it actually worked in your case with my solution. Probably something visual studio related, I guess.
Alright! It worked (a bit weird though because it was working fine when i had the whole code in one file).
I also had this problem when I started moving code around into multiple files. The real problem was that I forgot to write
#pragma once
at the top of the header file, and so it was redefining the functions multiple times (each time the header file was being invoked from a parent file), this caused the redefinition of default parameter error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With