Thread: 'operator <<' is ambiguous

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question 'operator <<' is ambiguous

    Trying a little exercise on overloading the <<. MSVC++ 6.0 will not compile because it says the << operator is ambiguous. It's not ambiguous to me, I know exactly what it's doing. But my compiler doesn't like it. Can anyone help ??

    Here is the prototype:
    Code:
    friend ostream& operator<<(ostream&, const IntegerSet&);

    Here is the function definition:
    Code:
    ostream& operator<<(ostream& output, const IntegerSet& Set) 
    { 
    	output << "\n{ "; 
    	for (int j = 0; j < Set.range; j++)   //range is a public variable of class IntegerSet
    	     if (Set.array[j] == 1) 
                   output << j << '\t';           //array is a public variable of class Integerset
    	output << "}" << endl; 
    	return output; 
    }


    Here is an example use of the overloaded operator:
    Code:
    cout << "set A = " << setA << endl;
    Last edited by The Brain; 06-01-2005 at 09:47 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    ambiguous calls have nothing to do with the defination of the function and everything to do with the prototypes.

    What is IntegerSet? What our functions are defined? Did the error say with what other functions is ambiguous with? (gcc does)

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    VC++ 6 has issues with friend classes where perfectly legitimate code will fail to compile. I recommend getting another compiler, because it's just not worth the effort to work around all of the problems with that one.
    My best code is written with the delete key.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I was able to resolve this by explicitly defining what parts of the std namespace I wanted to use. I'm guessing you've got a using namespace std; in there somewhere. If you change that to something like the following, it should compile.
    Code:
    using std::ostream;
    using std::endl;
    using std::cout;
    However, Prelude's completely right. Get a different compiler.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    how many << operators have you defined. what are they?
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    can you paste the compiler error returned by compiler out here
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    tried it in another compiler.. and it works fine..!!@#!!!
    Last edited by The Brain; 06-01-2005 at 11:29 AM. Reason: added more !@#$! for emphasis
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >!!@#!!!
    I feel your pain. I had to write code that would compile under multiple compilers, including VC++ 6, for some time. Thankfully, I don't have that particular restriction anymore. I can actually use C++ as it was meant to be used.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Help!! VC++ Problems
    By Astroboy in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2004, 01:24 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 04:07 PM