Like Tree1Likes
  • 1 Post By hk_mp5kpdw

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

This is a discussion on ambiguous overload for 'operator>>' in 'inData >> unit' within the C++ Programming forums, part of the General Programming Boards category; I try to read from a file but it keep giving me this error and i dont know what the ...

  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
    21,462
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,462
    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;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,462
    You're welcome
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,794
    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.
    Salem likes this.
    "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, 10:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21