I get errors at the bottom of the code at lines 176, 178, 180, and 182.
1 2 3 4 5 6 7 8 9 10 11 12 13
test.cc: In function 'int main()':
test.cc:176: error: no matching function for call to 'complex::complex(complex)'
test.cc:41: note: candidates are: complex::complex(complex&)
test.cc:176: error: initializing argument 1 of 'void complex::operator=(complex)'
test.cc:178: error: no matching function for call to 'complex::complex(complex)'
test.cc:41: note: candidates are: complex::complex(complex&)
test.cc:178: error: initializing argument 1 of 'void complex::operator=(complex)'
test.cc:180: error: no matching function for call to 'complex::complex(complex)'
test.cc:41: note: candidates are: complex::complex(complex&)
test.cc:180: error: initializing argument 1 of 'void complex::operator=(complex)'
test.cc:182: error: no matching function for call to 'complex::complex(complex)'
test.cc:41: note: candidates are: complex::complex(complex&)
test.cc:182: error: initializing argument 1 of 'void complex::operator=(complex)'
What do these errors mean? and how can I fix these?
Somebody correct me if I'm wrong; I mainly keep with C, mostly for things like this:
1 2 3
test.cc:176: error: no matching function for call to 'complex::complex(complex)'
test.cc:41: note: candidates are: complex::complex(complex&)
test.cc:176: error: initializing argument 1 of 'void complex::operator=(complex)'
You can see that it has an error with what is produced from the addition, or "argument 1 of ...". If you look it has no default constructor, so it could be the way it declares the new complex within the addition's definition, which is trying to call a default constructor.
I tried compiling this in my gcc, and received the same error as posted.
Tried making a default constructor, and had more errors. Looking back, my first post looks retarded.
Looking closer at the error, though, it appears the copy constructor is being called, but isn't being sent an address of a complex, just a complex.
Maybe it is called implicitly?
Anyway, the copy constructor is always supplied by the compiler unless explicitly overridden, so it does compile when you comment it out. Should work fine, just removing it.
Yes, a constructor is a copy constructor if and only iff it takes exactly one parameter by const reference (and the parameter is of the same type).
Ie, guestgulkan's version is a copy constructor; the one in the original post
is not.
There are many other problems with the code though (none that would prevent compilation, but are more usability concerns):
1. The copy constructor isn't needed; the default one provided will suffice;
2. The assignment operator, besides not returning a const reference and
taking its parameter by value rather than const reference, is also not
needed since the default one will suffice;
3. All of the member functions except setdata, operator=, and the friend
stream function should be const member functions;
4. All of the math operator functions should take their parameters by
const reference;
5. No default constructor is provided.