Ask about IO overloading

Please help me fix this error !!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <iomanip>

using namespace std;

class Vector2D{
    protected:
        int x, y;
    public:
        Vector2D(){
            this->x = 0;
            this->y = 0;
        }
        Vector2D(int x, int y){
            this->x = x;
            this->y = y;
        }
        Vector2D operator+(const Vector2D& p){
            Vector2D c;
            c.x = x + p.x;
            c.y = y + p.y;
            return c;
        }
        ostream &operator<<(ostream& os){
            os<<"("<<this->x<<", "<<this->y<<")";
            return os;
        }
};

int main()
{
    Vector2D ab(4, 5);
    cout<<ab;//causes error
    return 0;
}

In ostream &operator, i just use one param (because another is own this pointer). But I got error (i comment on code).
When I use two params, i still got error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

class Vector2D{
    protected:
        int x, y;
    public:
        Vector2D(){
            this->x = 0;
            this->y = 0;
        }
        Vector2D(int x, int y){
            this->x = x;
            this->y = y;
        }
        Vector2D operator+(const Vector2D& p){
            Vector2D c;
            c.x = x + p.x;
            c.y = y + p.y;
            return c;
        }
        ostream &operator<<(ostream& os, Vector2D &vector){//cause error
            os<<"("<<vector.x<<", "<<vector.y<<")";
            return os;
        }
};

int main()
{
    Vector2D ab(4, 5);
    cout<<ab;
    return 0;
}

I know exacctly why ? Because this function is member of class. If i put it outside class, no error. Why ? Does IO overloading only support for global function ?
Last edited on
operator<< should not be a member function. Making it a member function allows you to use << on a Vector2D object.
What you need is a friend function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Vector2D{
    protected:
        int x, y;
    public:
        Vector2D(){
            this->x = 0;
            this->y = 0;
        }
        Vector2D(int x, int y){
            this->x = x;
            this->y = y;
        }
        Vector2D operator+(const Vector2D& p){
            Vector2D c;
            c.x = x + p.x;
            c.y = y + p.y;
            return c;
        }
        // make this a friend
        friend ostream &operator<<(ostream& os, const Vector2D &vector){//cause error
            os<<"("<<vector.x<<", "<<vector.y<<")";
            return os;
        }
};
Last edited on
can you explain in more detail why it should be a friend and not a member, I thought that using << on a Vector2d was exactly what we wanted to achieve by overloading operator<<
Yes, I agree with quirkyusername (47) . Galik (1309) : I know why does it cause error ? But you can explain why ? Why other operators support overloading inside class, but IO operator doesn't. And I have other questions, why in this function, before operator have operator &.
When you make operator << a class member, it makes objects of that class receive the output like this:
1
2
3
Vector2D v1, v2;

v1 << v2;


If you want a different class of object to receive the output you have to define the function outside of any specific class. That way you get to specify the types of both sides of the expression:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class A
{
};

class B
{
};


A& operator<<(A& a, const B& b); // this allows a << b;

class C
{
    public:
    C& operator<<(A& a); // this allows (*this) << a;
    C& operator<<(B& a); // this allows (*this) << b;
};

// if I want to make C about to output to A (a << c) I need to either add a
// member function to A or make a non-class function

A& operator<<(A& a, const C& c); // this allows a << c;

// if I want to make C about to output to ostream (cout << c) I need to either add a
// member function to ostream (not possible) or make a non-class function

ostream& operator<<(ostream& os, const C& c); // this allows cout << c; 
Last edited on
Ok, and & sign, what does it mean ?
In this context (as a parameter qualifier) it means a reference. Parameters are normally copied when passed to a function and the function operates on the copies. But if you qualify them with an & then they are not copied and the function operates on the original.
Topic archived. No new replies allowed.