Operator Overloading << to print out vector

I am trying overload the operator << so that I can print out all my vectors in polly;

I have one error inside the polygon class, inside my operator overload function.

//Polygon does not define this operator or a conversion to a type acceptable to the predefined operator

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  #include <iostream>
using namespace std;
#include <vector>
class OnePoint {
private:
	double xvalue;
	double yvalue;

public:
	OnePoint(double x = 0.0, double y = 0.0) {
		xvalue = x;
		yvalue = y;
	}

	friend ostream& operator<<(ostream& printh, OnePoint& cPoint) {
		printh << "(" << cPoint.xvalue << ',' << cPoint.yvalue << ")";
		return printh;

	}

	void Plus(OnePoint a) {
		xvalue = xvalue + a.xvalue;
		yvalue = yvalue + a.yvalue;
	}

	void Minus(OnePoint b) {

		xvalue = xvalue + b.xvalue;
		yvalue = yvalue + b.yvalue;

	}

	OnePoint Plustwo(OnePoint a) {
		return (xvalue + a.xvalue, yvalue - a.yvalue);

	}

	void Change(double a, double b) {
		xvalue += a;
		yvalue += b;
	}

	void Print(OnePoint b) {

		cout << xvalue << "," << yvalue << endl;

	}

	/*OnePoint operator-(OnePoint a) {
		OnePoint temp;
		temp.xvalue = xvalue + a.xvalue;
		temp.yvalue = yvalue + a.yvalue;

		return temp;

	}


	friend OnePoint operator+(OnePoint a, OnePoint b) {
		OnePoint temp;
		temp.xvalue = a.xvalue + b.xvalue;
		temp.yvalue = a.yvalue + b.yvalue;

		return temp;

	}*/

	
};



class Line {
private: 
	OnePoint onevalue; 
	OnePoint twovalue; 
public: 
	Line(OnePoint a = OnePoint(), OnePoint b = OnePoint ()) {
		onevalue = a; 
		twovalue = b; 

	}

	OnePoint getonevalue() {
		return onevalue; 
	}

	OnePoint gettwovalue() {
	
		return twovalue; 
	}

	friend ostream& operator<<(ostream& print, Line& cLine){
		print << "{"<< cLine.onevalue << ',' << cLine.twovalue << "}"; 
		return print; 
	}

	/*friend Line operator+(OnePoint a, OnePoint b) {
		Line temp(a, b);                                    
		return temp;
	}*/


};

class Polygon{
private: 
	vector <OnePoint> polly; 
public: 
	Polygon(OnePoint a = OnePoint(), OnePoint b = OnePoint(), OnePoint c = OnePoint()) {
		polly.push_back(a); 
		polly.push_back(b); 
		polly.push_back(c); 
	}

	friend ostream& operator<<(ostream& print, Polygon& polly) {
		for (unsigned int i = 0; i < polly.polly.size(); i++) {
			print << polly[i]; // ERROR HERE. Polygon does not define this operator or a conversion to a type acceptable to the predefined operator
		}

		return print; 
	}
	

	void outputvector() {
	
		for (unsigned int i = 0; i < polly.size(); i++) {
			cout << polly[i]; 
		}
		

	}


}; 


Line operator+(const OnePoint &lhs, const OnePoint &rhs)
{
	Line retval(lhs, rhs);
	return retval;
}




int main(){

	

	OnePoint a(3.0, 3.0); 
	OnePoint b(1.0, 1.0);  
	OnePoint y(3.0, 4.0); 
	Line d(a, b); 
	Polygon j(a, b, y); 

	Line o;
	o = a + b; 

	j.outputvector();

	cout << j; 

	cout << a << endl;

	cout << d << endl; 

	cout << o << endl; 
 
	}
Your OnePoint class is missing an operator<< - not sure why the error mentions Polygon.
closed account (48T7M4Gy)
Try this: it appears to be a naming conflict ( sort of )
1
2
3
4
5
6
7
friend ostream& operator<<(ostream& print, Polygon& poly) {
   for (unsigned int i = 0; i < poly.polly.size(); i++) {
      print << poly.polly[i];
   }

return print; 
}


(3,3)(1,1)(3,4)(3,3)(1,1)(3,4)(3,3)
{(3,3),(1,1)}
{(3,3),(1,1)}
 
Last edited on
I think the error is that polygon doesn't define operator[], not operator<<. In your operator<< polly refers to the Polygon passed to the function. kemort provides a solution (although it is not for a naming conflict.)
closed account (48T7M4Gy)
Getting a bit pedantic and I wasn't seeking approval or otherwise from the gallery. I am referring to the confusion caused by having 'poly' and 'polly,where both refer to the same thing. The operator[] error message results from that naming problem and has nothing to do with the presence or absence of a [] operator.
Last edited on
kemort wrote:
Getting a bit pedantic... The operator[] error message results from that naming problem and has nothing to do with the presence or absence of a [] operator.


Pedantically, the operator[] error message has everything to do with the absence of said operator. I have no doubt the naming caused confusion for the OP, but it was perfectly legal and and did not need to be changed for your solution to work.

The following is semantically correct:
1
2
3
4
5
6
7
friend ostream& operator<<(ostream& print, Polygon& polly) {
   for (unsigned int i = 0; i < polly.polly.size(); i++) {
      print << polly.polly[i];
   }

   return print; 
}


I might have suggested to the OP the following change to the member output_vector since it's there (although I don't really see why it's a member of Polygon.):

1
2
3
4
5
   void outputvector(ostream& os) {
      for (unsigned int i = 0; i < polly.size(); i++) {
         os << polly[i]; 
      }
   }


And then implement operator<< (which would no longer need to be a friend function) in terms of it:

1
2
3
4
ostream& operator<<(ostream& print, Polygon& p) {
   p.output(print);
   return print;
}


[Edit: And I should note the code needs some adjustment to make it const correct.]

Last edited on
closed account (48T7M4Gy)
And the point of all your waffle is?
And the point of all your waffle is?

That your waffle had none.

And, to keep from being as petty as you, I'll provide one more bit of code for the OP in support of that thought:

One can make the original overloaded operator<< work by supplying Polygon with an operator[].

1
2
3
4
5
6
7
8
9
10
11
12
13
    //...

    OnePoint operator[](std::size_t index) { return polly[index]; }

    friend ostream& operator<<(ostream& print, Polygon& polly) {
        for (unsigned int i = 0; i < polly.polly.size(); i++) {
            print << polly[i]; 
        }

        return print;
    }

    // ... 
Topic archived. No new replies allowed.