istream and ostream operators of Vector

Hi
I could not write istream and ostream operators of Vector
How should I write.


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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
struct floatt{
	float f[4];
	floatt()
	{
		for (int k =  0; k < 4; k++)
			f[k] = 0.0;
	}
	floatt(float f1, float f2, float f3, float f4)
	{
		f[0] = f1;
		f[1] = f2;
		f[2] = f3;
		f[3] = f4;
	}

	floatt(const floatt &r)
	{

	for (int k = 0; k < 4; k++)
		f[k] = r.f[k];

	}

	floatt &operator=(const floatt &r)
	{
		
	for (int k = 0; k < 4; k++)
		f[k] = r.f[k];
	return *this;
	}

	float operator[](int index)
	{
		return f[index];
	}

	void display()const
	{
		cout << "(";
		for (int k = 0; k < 4; k++)
			cout << f[k] << " ";
		cout << ")" << endl;
	}


friend ostream &operator<<(ostream &os, const floatt &v);
friend istream &operator>>(istream &is, floatt &v);

};


ostream &operator<<(ostream &os, const floatt &f)
{
	return os  << "(" << f.f[0] << ", " << f.f[1] << ", " << f.f[2] << ", " << f.f[3]<< ")";
}
istream &operator>>(istream &is, floatt &f)
{
	return is >> f.f[0] >> f.f[1] >> f.f[2] >> f.f[3];
}




class Vector{
	floatt *ff;
	int size;
public:
	Vector(unsigned int n)
	{
		size = n;
		ff = new floatt[size];

		for (int i = 0; i < size; i++)
			for (int k = 0; k < 4; k++)
				ff[i].f[k] = 0.0;
	}

	~Vector()
	{
		delete [] ff;
	}

	Vector(const Vector &r)
	{
		
		ff = new floatt[r.size];
		size  = r.size;
		for (int k = 0; k < size; k++)
			ff[k] = r.ff[k];
	}


	Vector &operator=(const Vector &r)
	{
		delete []ff;
		 if (size < 0)
			size  = r.size;
		ff = new floatt[size];
		for (int k = 0; k < size; k++)
			ff[k] = r.ff[k];

		return *this;
		
	}

	floatt operator[](int index)
	{
			 return this->ff[index];
	}


	void display()const
	{
		for (int k = 0; k < this->size; k++)
			ff[k].display();

	}

	
	void set_Vector(int x)
	{

		for (int k = 0; k < this->size; k++)
			for (int i = 0; i < 4; i++)
				ff[k].f[i] = rand() % x + (3.4); 

	}


	Vector operator+(const Vector &r)const
	{
		Vector result = *this;

		for (int k = 0; k < r.size; k++)
			for (int i = 0; i < 4; i++)
				result.ff[k].f[i] += r.ff[k].f[i];
		return result;
	}


	Vector operator-(const Vector &r)const
	{
		Vector result = *this;

		for (int k = 0; k < r.size; k++)
			for (int i = 0; i < 4; i++)
				result.ff[k].f[i] -= r.ff[k].f[i];
		return result;
	}

	Vector operator/(const Vector &r)const
	{
		Vector result = *this;

		for (int k = 0; k < r.size; k++)
			for (int i = 0; i < 4; i++)
				result.ff[k].f[i] /= r.ff[k].f[i];
		return result;
	}

		Vector operator*(const Vector &r)const
	{
		Vector result = *this;

		for (int k = 0; k < r.size; k++)
			for (int i = 0; i < 4; i++)
				result.ff[k].f[i] *= r.ff[k].f[i];
		return result;
	}



friend ostream &operator<<(ostream &os, const Vector &v);
friend istream &operator>>(istream &is, Vector &v);


};

ostream &operator<<(ostream &os,  const Vector &v)
{
	return os << v.ff->f;
}
istream &operator>>(istream &is,  Vector &v)
{

	return is >> v.ff->f;
}


int main()
{
	Vector v(10);


	cin >> v[1];



	cout << v[1] << endl;


	return 0;		
}
Last edited on
Problem 1:

1
2
3
4
5
6
istream &operator>>(istream &is,  Vector &v)
{

    return is >> v.ff->f; //error  -  v.ff::f  is an array of floats  
}



problem 2:
In the Vector class:
1
2
3
4
5
    floatt operator[](int index) //error- returning a rvalue
    {
             return this->ff[index];
    }


and in the floatt class:
1
2
3
4
    float operator[](int index) //error - returning a rvalue
    {
        return f[index];
    }


If you want your [] operator overload to be used on both sides of a assignment - you should return a reference.
So:
floatt & operator[](int index) //return a reference - which can be used as both lvalue and rvalue
and
float & operator[](int index)//return a reference - which can be used as both lvalue and rvalue
1
2
3
4
5
istream &operator>>(istream &is,  Vector &v)
{

    return is >> v.ff->f; //error  -  v.ff::f  is an array of floats  
}



What can I do instead.
1
2
3
4
5
6
7
8
ostream &operator<<(ostream &os,  const Vector &v)
{
        return os << v.ff->f[0] <<  v.ff->f[1] << v.ff->f[2] << v.ff->f[3];
}
istream &operator>>(istream &is,  Vector &v)
{
       return is >> v.ff->f[0] >> v.ff->f[1] >> v.ff->f[2] >> v.ff->f[3];
}


Do you think ?
Probably do iostream
But vector hasn't just 4 floatt. Use a loop against its size
ok.

how do I ?


cin >> v;

cout << v;


How do I write istream and ostream operators ?
Well, the ff pointer member of Vector points to an array of floatt. You track the size of this array in the size member variable.

Each floatt object has an array of 4 float called f.

So as you can see you can use a nested loop:
The outer loop to loop through the floatt array and the inner loop to loop through
the f array of each floatt
@guestgulkan

I use already with other operator functions.

example:
1
2
3
4
5
6
7
8
9
Vector operator-(const Vector &r)const
{
	Vector result = *this;

	for (int k = 0; k < r.size; k++)
                      for (int i = 0; i < 4; i++)
	            result.ff[k].f[i] -= r.ff[k].f[i];
	      return result;
}


1
2
3
4
5
6
ostream &operator<<(ostream &os,  const Vector &v)
{
        for (int k = 0; k < r.size; k++)
              for (int i = 0; i < 4; i++)

 }
what I will return ?
Last edited on
Is not necessary to do it all in one line
1
2
3
4
5
6
ostream &operator<<(ostream &os,  const Vector &v)
{
        for (int k = 0; k < r.size; k++)
              os << r.ff[k]; //this modifies your ostream object
         return os; //what else will you return?
 }
Last edited on
my code is the last case

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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
struct floatt{
	float f[4];
	floatt()
	{
		for (int k =  0; k < 4; k++)
			f[k] = 0.0;
	}
	floatt(float f1, float f2, float f3, float f4)
	{
		f[0] = f1;
		f[1] = f2;
		f[2] = f3;
		f[3] = f4;
	}

	floatt(const floatt &r)
	{

	for (int k = 0; k < 4; k++)
		f[k] = r.f[k];

	}

	floatt &operator=(const floatt &r)
	{
		
	for (int k = 0; k < 4; k++)
		f[k] = r.f[k];
	return *this;
	}

	float &operator[](int index)
	{
		return f[index];
	}

	void display()const
	{
		cout << "(";
		for (int k = 0; k < 4; k++)
			cout << f[k] << " ";
		cout << ")" << endl;
	}


friend ostream &operator<<(ostream &os, const floatt &r);
friend istream &operator>>(istream &is, floatt &r);

};


ostream &operator<<(ostream &os, const floatt &r)
{
	cout << "(";
	for (int i = 0; i < 4; i++)
		os << r.f[i] << " ";
	cout << ")" << endl;
	return os;

}
istream &operator>>(istream &is, floatt &r)
{
	for (int i = 0; i < 4; i++)
		is >> r.f[i];
	return is;
	
}




class Vector{
	floatt *ff;
	int size;
public:
	Vector(unsigned int n)
	{
		size = n;
		ff = new floatt[size];

		for (int i = 0; i < size; i++)
			for (int k = 0; k < 4; k++)
				ff[i].f[k] = 0.0;
	}

	~Vector()
	{
		delete [] ff;
	}

	Vector(const Vector &r)
	{
		
		ff = new floatt[r.size];
		size  = r.size;
		for (int k = 0; k < size; k++)
			ff[k] = r.ff[k];
	}


	Vector &operator=(const Vector &r)
	{
		delete []ff;
		 if (size < 0)
			size  = r.size;
		ff = new floatt[size];
		for (int k = 0; k < size; k++)
			ff[k] = r.ff[k];

		return *this;
		
	}

	floatt &operator[](int index)
	{
			 return this->ff[index];
	}


	void display()const
	{
		for (int k = 0; k < this->size; k++)
			ff[k].display();

	}

	
	void set_Vector(int x)
	{

		for (int k = 0; k < this->size; k++)
			for (int i = 0; i < 4; i++)
				ff[k].f[i] = rand() % x + (3.4); 

	}


	Vector operator+(const Vector &r)const
	{
		Vector result = *this;

		for (int k = 0; k < r.size; k++)
			for (int i = 0; i < 4; i++)
				result.ff[k].f[i] += r.ff[k].f[i];
		return result;
	}


	Vector operator-(const Vector &r)const
	{
		Vector result = *this;

		for (int k = 0; k < r.size; k++)
			for (int i = 0; i < 4; i++)
				result.ff[k].f[i] -= r.ff[k].f[i];
		return result;
	}

	Vector operator/(const Vector &r)const
	{
		Vector result = *this;

		for (int k = 0; k < r.size; k++)
			for (int i = 0; i < 4; i++)
				result.ff[k].f[i] /= r.ff[k].f[i];
		return result;
	}

		Vector operator*(const Vector &r)const
	{
		Vector result = *this;

		for (int k = 0; k < r.size; k++)
			for (int i = 0; i < 4; i++)
				result.ff[k].f[i] *= r.ff[k].f[i];
		return result;
	}



friend ostream &operator<<(ostream &os, const Vector &v);
friend istream &operator>>(istream &is, Vector &v);


};

ostream &operator<<(ostream &os,  const Vector &r)
{
	for (int k = 0; k  < r.size; k++)
		os << r.ff[k];
	return os;
}
istream &operator>>(istream &is,  Vector &r)
{
	for (int k = 0; k < r.size; k++)
		is >> r.ff[k];
	return is;

}
Last edited on
thanks...
Topic archived. No new replies allowed.