1

Possible Duplicate:
Constructors with default parameters in Header files
Default value of function parameter

ERROR:

**** Internal Builder is used for build               ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\Calculator.o ..\src\Calculator.cpp
..\src\Calculator.cpp:26:55: error: default argument given for parameter 1 of 'CComplex::CComplex(float, float)'
..\src\/Calculator.h:25:9: error: after previous specification in 'CComplex::CComplex(float, float)'
..\src\Calculator.cpp:26:55: error: default argument given for parameter 2 of 'CComplex::CComplex(float, float)'
..\src\/Calculator.h:25:9: error: after previous specification in 'CComplex::CComplex(float, float)'
Build error occurred, build is stopped
Time consumed: 563  ms.

Did anyone encounter similar issue. What could be possible work around?

Community
  • 1
  • 1
Harsha
  • 5
  • 1
  • 5
  • My guess is that you specify the default argument in the function definition; you shouldn't. It would help to see the code. – Beta Nov 26 '12 at 00:34
  • [http://stackoverflow.com/questions/1440222/constructors-with-default-parameters-in-header-files](http://stackoverflow.com/questions/1440222/constructors-with-default-parameters-in-header-files) – melpomene Nov 26 '12 at 00:34
  • Looks like the thing to look at would be the declarations of the constructor `CComplex::CComplex` in `Calculator.h` and `Calculator.cpp`. Could you post the code please? – Brian L Nov 26 '12 at 00:40
  • 1
    I bet it's `class CComplex { CComplex(float r, float i = 0.0); };` and `CComplex::CComplex(float r, float i = 0.0) {}`, respectively. – melpomene Nov 26 '12 at 00:42

2 Answers2

1

You are doing it wrong with default argument in function definition.

class {
void CComplex(float a=0.0, floatb=0.0);
};

If you have such function definition then it's wrong:

void CComplex::CComplex(float a=0.0, floatb=0.0) 
{
}

it should be:

void CComplex(float a, float) 
{
}

then

call CComplex(); `a,b` will be default to `0.0`
call CComplex(1.0); will set a to a 1.0 and b to 0.0
call CComplex(1.0, 2.0); will set a to 1. and b to 2.0
billz
  • 44,644
  • 9
  • 83
  • 100
  • You've answered something entirely different then edited you question to match mine... and it's still a mess. – jaho Nov 26 '12 at 01:02
  • But if I declare as above, I am not able to pass the second parameter to the set function. // CComplex(float real = 0, float imaginary = 0); // Default values to declaration // void set(float real, float imaginary); CComplex (CComplex const &c); float abs(); void print(); CComplex& operator++(); CComplex operator++(int); // – Harsha Nov 26 '12 at 01:17
1

You need to declare default values for function argument in function declaration (usually in the header file), but not definition (usually cpp file). So in your case the code should look something like this:

in .h file

CComplex(float r=0.0, float i=0.0);

in .cpp file

CComplex::CComplex(float r, float i)
{
    // ...
}
jaho
  • 4,852
  • 6
  • 40
  • 66