Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The compiler is complaining about my default parameters?

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

like image 684
Christian Avatar asked Jun 02 '11 04:06

Christian


People also ask

How do you set a default parameter?

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 .

What do you mean by default parameters explain with the help of suitable example?

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.

Does C support default parameter?

No, Standard C does not support either.

Can you set default parameters in Java?

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.


2 Answers

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.

like image 199
Mahesh Avatar answered Sep 29 '22 09:09

Mahesh


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.

like image 22
MasterHD Avatar answered Sep 29 '22 10:09

MasterHD