Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,550 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,574 people online right now. Registration is fast and FREE... Join Now!




Overloading input operator

 
Reply to this topicStart new topic

Overloading input operator, getting "ambiguous overload" error

lwnexgen
21 Nov, 2007 - 11:10 PM
Post #1

New D.I.C Head
*

Joined: 27 Mar, 2007
Posts: 12


My Contributions
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.h

Here'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 tongue.gif

This post has been edited by lwnexgen: 21 Nov, 2007 - 11:25 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Overloading Input Operator
21 Nov, 2007 - 11:16 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Well, just as a guess, I'd say it has to do with the fact that you're trying to input an endl object as well. >> isn't defined for endl (I mean, what would it do?), so it would matter if you were trying to extract even just a plain old integer from the stream, it still wouldn't work.

If that doesn't help, I'll take another look at the bulk of the code. But that's what caught my eye first.

-jjh
User is offlineProfile CardPM
+Quote Post

lwnexgen
RE: Overloading Input Operator
21 Nov, 2007 - 11:22 PM
Post #3

New D.I.C Head
*

Joined: 27 Mar, 2007
Posts: 12


My Contributions
QUOTE(jjhaag @ 22 Nov, 2007 - 12:16 AM) *

Well, just as a guess, I'd say it has to do with the fact that you're trying to input an endl object as well. >> isn't defined for endl (I mean, what would it do?), so it would matter if you were trying to extract even just a plain old integer from the stream, it still wouldn't work.

If that doesn't help, I'll take another look at the bulk of the code. But that's what caught my eye first.

-jjh



Hmm, good catch (spending too much time coding in java lately, didn't notice that one), but main is still throwing the ambiguous overload.

Do you by chance have a quick explanation of what an ambiguous overload is? (ie missing return value, or something along those lines?)

Thanks!
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Overloading Input Operator
21 Nov, 2007 - 11:57 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Okay, now I think I see it. When you declare your ComplexNumber obect C1...you're actually not doing that at all. You're creating a function named C1 that takes no arguments and returns a ComplexNumber object. To create an object with the default constructor, you don't use the parentheses. The following should sort of work:
CODE
    ComplexNumber C1;
    cin >> C1;


I say sort of, because you haven't defined the rest of your operators, and you'll need them to do this (you use the overloaded assignment operator = in your definition of >> for ComplexNumber, so you'll need at least that filled in).

This post has been edited by jjhaag: 21 Nov, 2007 - 11:58 PM
User is offlineProfile CardPM
+Quote Post

lwnexgen
RE: Overloading Input Operator
22 Nov, 2007 - 12:06 AM
Post #5

New D.I.C Head
*

Joined: 27 Mar, 2007
Posts: 12


My Contributions
QUOTE(jjhaag @ 22 Nov, 2007 - 12:57 AM) *

Okay, now I think I see it. When you declare your ComplexNumber obect C1...you're actually not doing that at all. You're creating a function named C1 that takes no arguments and returns a ComplexNumber object. To create an object with the default constructor, you don't use the parentheses. The following should sort of work:
CODE
    ComplexNumber C1;
    cin >> C1;


I say sort of, because you haven't defined the rest of your operators, and you'll need them to do this (you use the overloaded assignment operator = in your definition of >> for ComplexNumber, so you'll need at least that filled in).


Ah, excellent. That gets me to compile at least, now to debug my overloaded function.

Thanks!
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Overloading Input Operator
22 Nov, 2007 - 06:23 AM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
Good catch jjhaag! I was looking in entirely the wrong direction.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:45PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month