"cout <<" error

This is a discussion on "cout <<" error within the C++ Programming forums, part of the General Programming Boards category; ok im trying to fix the code of three classes and i've managed to fix every error but 4. all ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    224

    "cout <<" error

    ok im trying to fix the code of three classes and i've managed to fix every error but 4. all 4 errors are the same just in different spots of 2 functions in a class. the errors are

    Code:
    D:\classesexamples\geometry\Box.cpp(49) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Point' (or there is no acceptable conversion)
    and the source code that it points to is

    Code:
    void Box::getCorners(Point& a, Point& b) {
         corner1 = a; 
         corner2 = b;
         cout << corner1 << endl;
         cout << corner2 << endl;
    }
    
    void Box::print() {
         cout << corner1 << endl;
         cout << corner2 << endl;
    }
    i just dont get why it says there is and error with <<

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    cout doesnīt know how it should display Point. Try overload operator << for class Point (I assume that corner1 and corner2 are instances of class Point).
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    could you explain what you mean by the overload operator <<

    and the values corner1 and corner2 are supposed to represent x and y values that are private members in the class Point.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    8
    Hi. For one of the projects in my CS class this semester we had to overload the >> and << operators for a complex # class. The reason we had to do that was because the class complex creates a new data type, Complex. so when u did the following
    Code:
    Complex a;
    cout << a;
    you would get the same error
    D:\classesexamples\geometry\Box.cpp(49) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Point' (or there is no acceptable conversion)
    - cout doesnt know how to display the data type Complex. Your problem is that cout doesn't know how to display the Point data type, so you have to tell it how by overloading the << operator.

    To do that you have to make the << operator a friend of the class like this
    Code:
    class Point{
      public:
        friend ostream& operator<< (ostream& os, Point& x);
        // whatever else
    };
    after that u have to tell the << operator what to output like this
    Code:
    ostream& operator<< (ostream& os, Point& x)
    {
    os << x; // make this whatever you want to output
    return os;
    }
    Hopefully all thats right (someone more experienced correct if not). Hope that helps.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    << should have a constant second parameter:
    friend ostream& operator<< (ostream& os, const Point& x);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    8
    Oops. Forgot that. Thanks for the correction.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    thx for the reply but i got a few questions seeing im not all that good or experienced at c++ especially classes

    what does friend mean or represent and can i be called anything and why is it there?

    and after all that to use it i just use "<<" nothing else or is it "operator<<" that i use

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The friend keyword allows the so-marked class or function to access all member of the class that contains the friend-declaration, even the protected and private members.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    so when i got all that how do i use the << operator? the same way?

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Overloading operators is basically just defining a new function. Except, you can't just put the operator itself, since that would be... an operator. So you use the 'operator' keyword before the operator itself, and then the operator, then the argument list, etc.

    That's why it's 'operator <<' instead of just '<<' when you define it.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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