Hey Guys-
I need to overload the input (>>) operator for a class, and I keep getting a strange "ambiguous overload error" that I don't understand.
Here's a link to the header file (we were given it, so I'm not able to change it around at all):
http://pages.cs.wisc.edu/~cs368-1/assignme...ComplexNumber.hHere's my overloaded operator function:
CODE
//Constructors, in case they're useful
ComplexNumber::ComplexNumber() : real(0.0),imag(0.0) {}
ComplexNumber::ComplexNumber(double real_part, double imaginary_part) : real(real_part),imag(imaginary_part) {}
ComplexNumber::ComplexNumber(const ComplexNumber & rhs) : real(rhs.real),imag(rhs.imag) {}
//End constructors
//Overload >> for input
istream & operator>>(istream & in, ComplexNumber & n)
{
char realStream[30] = {' '};
char imagStream[30] = {' '};
char thisChar = '!';
//Hold the input stream
char input[64];
//fill the input stream character array with the input
in.getline(input, 64);
for (int i = 0; i < 64; i++)
{
thisChar = input[i];
if ((thisChar == '+')||(thisChar == '-'))
{
for (int before = 0; before < (i-1); before++)
{
realStream[before] = input[before];
}
for (int after = i + 1; after < 64; after++)
{
imagStream[after] = input[after];
}
}
}
double newReal = atof(realStream);
double newImag = atof(imagStream);
if (thisChar == '-')
{
newImag = (-1)*(newImag);
}
ComplexNumber newComplex(newReal,newImag);
n = newComplex;
return in;
}
and here is the main function:
CODE
int main()
{
ComplexNumber C1();
cin >> C1; //Changed to reflect jjh's idea
return 0;
}
The "error: ambiguous overload for ‘operator>>’ in ‘std::cin >> C1’" obviously pops up at the cin line. Any ideas? Hopefully you guys can offer a bit more help than my wonderful, "I'm just learning C++ as well" professor
This post has been edited by lwnexgen: 21 Nov, 2007 - 11:25 PM