Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2008
    Posts
    3

    Overloading << operator

    Hi there,

    My name is Ciprian, I've been watching the C++ video tutorials. On my computer I have installed Microsoft Visual C++ 6.0 and it behaves different when I try to overload the << operator. I've written the same example which you explain into the Classes lesson. In your example works fine, however, when I try to compile it I get the following error message:

    error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Point' (or there is no acceptable conversion)

    Same thing happens when I try to overload the >> operator. Can you guys give me a hint why it is behaving like this?

    Thanks in advance.

  2. #2
    Join Date
    Jul 2008
    Location
    Oak Lawn, IL
    Posts
    8
    did you create the overloaded function prototype inside the Point class or not!!

    could you post some of the code plz..

  3. #3
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    Are you sure you did the ostream << overload they demonstrated?
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

  4. #4
    Join Date
    Apr 2008
    Posts
    3
    Whether I create the overloaded function prototype or not, I still get the same message. Anyway, they didn't.

    This is my code:


    #include <iostream.h>


    class Point
    {

    public:
    Point(float fx = 1, float fy = 1, float fz = 1);
    ~Point();
    float getX();
    float getY();
    float getZ();
    void Point::getXYZ(float &X, float &Y, float &Z);

    void setX(float X);
    void setY(float Y);
    void setZ(float Z);
    void setXYZ(float X, float Y, float Z);
    Point operator+ (Point p);

    private:
    float x, float y, float z;
    };


    Point::Point(float fx, float fy, float fz)
    {

    // cout << "We're in the constructor with arguments" << (int)this << endl;
    x = fx;
    y = fy;
    z = fz;
    }

    Point::~Point()
    {}

    float Point::getX()
    {
    return x;
    }

    float Point::getY()
    {
    return y;
    }

    float Point::getZ()
    {
    return z;
    }

    void Point::getXYZ(float &X, float &Y, float &Z)
    {
    X = getX();
    Y = getY();
    Z = getZ();
    }


    void Point::setX(float X)
    {
    x = X;
    }

    void Point::setY(float Y)
    {
    y = Y;
    }

    void Point::setZ(float Z)
    {
    z = Z;
    }

    void Point::setXYZ(float X, float Y, float Z)
    {
    setX(X);
    setY(Y);
    setZ(Z);
    }

    Point Point:perator+ (Point p) // c = a.operator+ (b)
    {
    x = x + p.x;
    y = y + p.y;
    z = z + p.z;
    return *this;
    }

    ostream &operator <<(ostream &stream, Point &p)
    {
    stream << p.getX() << " " << p.getY << " " << p.getZ();
    return stream;
    }

    /* istream &operator >> (istream &stream, Point &p)
    {
    float x, y, z;
    stream >> x >> y >> z;
    p.setXYZ(x, y, z);
    return stream;
    } */


    main()
    {
    float x, y, z;
    Point Point1(10, 20, 30);
    Point Point2;
    Point Point3 = Point1 + Point2; //This is equivalent with: Point Point3 = Point1.operator+ (Point2);

    cout << Point3 << endl; //This is where I get the error message.

    cout << Point3.getX() << endl;
    cout << Point3.getY() << endl;
    cout << Point3.getZ() << endl;
    }

  5. #5
    Join Date
    Nov 2007
    Posts
    8
    ostream &operator <<(ostream &stream, Point &p)
    {
    stream << p.getX() << " " << p.getY() << " " << p.getZ();
    return stream;
    }


    (need parens after method call to getY). Although you 'should' have seen other errors prior to the one you indicated.

  6. #6
    Join Date
    Nov 2007
    Posts
    8
    There were a few other typos...also, I'm not sure when MS changed, but all *streams now are in the std namespace and requires std:: preceding it (or 'using namespace std' at the top, which I used). This one compiled clean for me (minimal changes to the code you posted).

    #include <iostream>
    using namespace std;

    class Point
    {
    private:
    float x;
    float y;
    float z;
    public:
    Point(float fx = 1, float fy = 1, float fz = 1);
    ~Point();
    float getX();
    float getY();
    float getZ();
    void Point::getXYZ(float &X, float &Y, float &Z);

    void setX(float X);
    void setY(float Y);
    void setZ(float Z);
    void setXYZ(float X, float Y, float Z);
    Point operator+ (Point p);

    };


    Point::Point(float fx, float fy, float fz)
    {

    // cout << "We're in the constructor with arguments" << (int)this << endl;
    x = fx;
    y = fy;
    z = fz;
    }

    Point::~Point()
    {}

    float Point::getX()
    {
    return x;
    }

    float Point::getY()
    {
    return y;
    }

    float Point::getZ()
    {
    return z;
    }

    void Point::getXYZ(float &X, float &Y, float &Z)
    {
    X = getX();
    Y = getY();
    Z = getZ();
    }


    void Point::setX(float X)
    {
    x = X;
    }

    void Point::setY(float Y)
    {
    y = Y;
    }

    void Point::setZ(float Z)
    {
    z = Z;
    }

    void Point::setXYZ(float X, float Y, float Z)
    {
    setX(X);
    setY(Y);
    setZ(Z);
    }

    Point Point:perator+ (Point p) // c = a.operator+ (b)
    {
    x = x + p.x;
    y = y + p.y;
    z = z + p.z;
    return *this;
    }

    ostream &operator<<(ostream &stream, Point &p)
    {
    stream << p.getX() << " " << p.getY() << " " << p.getZ();
    return stream;
    }

    /* istream &operator >> (istream &stream, Point &p)
    {
    float x, y, z;
    stream >> x >> y >> z;
    p.setXYZ(x, y, z);
    return stream;
    } */


    int main()
    {
    Point Point1(10, 20, 30);
    Point Point2;
    Point Point3 = Point1 + Point2; //This is equivalent with: Point Point3 = Point1.operator+ (Point2);

    cout << Point3 << endl; //This is where I get the error message.

    cout << Point3.getX() << endl;
    cout << Point3.getY() << endl;
    cout << Point3.getZ() << endl;
    }

  7. #7
    Join Date
    Apr 2008
    Posts
    3
    Thanks a lot, it was childish from me not to notice those typos over and over. Works fine now.

  8. #8
    Join Date
    Nov 2007
    Posts
    8
    Childish ? pfft...in my book, its not a compiler..its a spellchecker

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •