Thread: ambiguous overload for 'operator>>' in 'inData >> unit'

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    9

    ambiguous overload for 'operator>>' in 'inData >> unit'

    I try to read from a file but it keep giving me this error and i dont know what the error mean. I try and google it but didnt see any solution.
    Code:
    #include<iostream>#include<iomanip>
    #include<fstream>
    
    
    using namespace std;
    
    
    int main()
    {
        string iteam;
        int unit();
        double unitCost(), totalValue();
        ifstream inData;
        ofstream outData;
    
    
        inData.open("data.txt");
        while(!inData.eof())
        {
            inData >> unit;
            cout << unit;
        }
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This declares a function named unit that takes no arguments and returns an int:
    Code:
    int unit();
    You probably wanted to declare a variable named unit of type int:
    Code:
    int unit;
    Likewise for unitCost and totalValue.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    9
    oh wow it worked!
    Thanks a lot.
    but one more question if you can answer.
    My professor put those parentheses to initialize the value (he said it was the same as int unit = 0), but why doesnt it work here?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by robinnbastar
    My professor put those parentheses to initialize the value (he said it was the same as int unit = 0), but why doesnt it work here?
    Because in this context, it looks like a function declaration, hence it is a function declaration. If you had written:
    Code:
    int unit = int();
    then it would be a variable definition that initialises unit to zero, though I find it simpler in this case to write:
    Code:
    int unit = 0;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    9
    Thanks a lot. you're a big help

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're welcome
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    inData.open("data.txt");
    while(!inData.eof())
    {
        inData >> unit;
        cout << unit;
    }
    Do not use an end-of-file test to control your loops. It is rarely done correctly. You should instead prefer a test of the read operation for failure, in your case I would suggest the following loop:

    Code:
    inData.open("data.txt");
    while(inData >> unit)
    {
        cout << unit;
    }
    Here the stream extraction will return the stream itself which when tested in such a context will basically yield a true/false result on the state of the stream. If the inData stream goes into an error state - as if there is no more data to be read from the file - then the result of the extraction will evaluate to false in this context and the loop will exit.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ambiguous overload for operators...
    By Cobs in forum C++ Programming
    Replies: 8
    Last Post: 05-11-2011, 08:44 PM
  2. Replies: 2
    Last Post: 03-31-2010, 08:29 PM
  3. ambiguous overload with derived classes?
    By Sebastiani in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2008, 10:14 PM
  4. 'operator <<' is ambiguous
    By The Brain in forum C++ Programming
    Replies: 7
    Last Post: 06-01-2005, 12:32 PM
  5. ambiguous << operator, overload not right?
    By neandrake in forum C++ Programming
    Replies: 7
    Last Post: 11-14-2004, 11:18 PM