Help with errors for Overloading Operator

Hi,
I have to create three dimension vector and currently have many errors.

I have got about 30 errors for following error from main.cpp
Error 1-30 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Vector' (or there is no acceptable conversion)

rest of errors are from vector.h
Error 1 error C2143: syntax error : missing ';' before '&' c:\users\soulpower55\documents\visual studio 2013\projects\lab4-cs215\lab4-cs215\vector.h 73 1 lab3-CS215
Error 2 error C2433: 'ostream' : 'friend' not permitted on data declarations c:\users\soulpower55\documents\visual studio 2013\projects\lab4-cs215\lab4-cs215\vector.h 73 1 lab3-CS215
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\soulpower55\documents\visual studio 2013\projects\lab4-cs215\lab4-cs215\vector.h 73 1 lab3-CS215
Error 4 error C2061: syntax error : identifier 'ostream' c:\users\soulpower55\documents\visual studio 2013\projects\lab4-cs215\lab4-cs215\vector.h 73 1 lab3-CS215
Error 5 error C2805: binary 'operator <<' has too few parameters






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

//vector.h//
#ifndef vector_h
#define vector_h
// INCLUDE FILES
#include <iostream>
//
//=============================================================================
// CONSTANT DEFINITION
// none
//=============================================================================
// EXTERNAL CLASS VARIABLE
// none
//=============================================================================
// FUNCTION PROTOTYPES
// none
//=============================================================================
// Class Object
//
class Vector {
	//=============================================================================
public:
	Vector();
	Vector(double x, double y, double z);
	Vector(const Vector&);
	~Vector();

	// getters
	double getX() const {
		return pVec[0];
	}
	double getY() const {
		return pVec[1];
	}
	double getZ() const {
		return pVec[2];
	}

	// setters
	void setX(double x) {
		pVec[0] = x;
	}
	void setY(double y) {
		pVec[1] = y;
	}
	void setZ(double z) {
		pVec[2] = z;
	}

	// return magnitude of vector
	double magnitude() const {
		return sqrt(pVec[0] * pVec[0] + pVec[1] * pVec[1] + pVec[2] * pVec[2]);
	}

	Vector operator-();
	bool operator<(const Vector&);
	bool operator==(const Vector&);
	Vector &operator=(const Vector &rhs);

private:
	const size_t VDIMENSION;
	double *pVec;
	void doCopy(const Vector&);

	friend double operator*(const Vector&, const Vector&);
	friend Vector operator*(const Vector&, double);
	friend Vector operator*(double, const Vector&);
	friend Vector operator+(const Vector&, const Vector&);
	friend Vector operator+(const Vector&, double);
	friend Vector operator+(double, const Vector&);
	friend Vector operator-(const Vector&, const Vector&);
	friend Vector operator-(const Vector&, double);
	friend Vector operator-(double, const Vector&);
	friend Vector operator^(const Vector&, const Vector&);
	friend ostream& operator<<(ostream&, const Vector&);
};

//=============================================================================
// END OF CONDITIONAL BLOCK
#endif
//=============================================================================
// END OF HEADER FILE
//=============================================================================


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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//=============================================================================
// INCLUDE FILES
#include "vector.h"
#include <iostream>
using namespace std;
/*
* default constructor
* allocate memory for the three coordinates and initialize them to 0
*/
Vector::Vector()
: VDIMENSION(3) {
	pVec = new double[VDIMENSION];
	for (size_t i = 0; i < VDIMENSION; i++)
		pVec[i] = 0;
}

/*
* parameterized constructor
* allocate memory for the three coordinates and initialize them to the parameters
*/
Vector::Vector(double x, double y, double z)
: VDIMENSION(3) {
	pVec = new double[VDIMENSION];
	pVec[0] = x;
	pVec[1] = y;
	pVec[2] = z;
}

/*
* copy constructor
* performs deep copy
*/
Vector::Vector(const Vector &rhs)
: VDIMENSION(3) {
	pVec = new double[VDIMENSION];

	doCopy(rhs);
}

/*
* overloaded assignment operator
* performs deep copy assignment
*/
Vector &Vector::operator=(const Vector &rhs) {
	// check for self assignment
	if (this != &rhs)
		doCopy(rhs);

	return *this;
}

/*
* does the actual copying of rhs to *this object
*/
void Vector::doCopy(const Vector &rhs) {

	// assign coordinates
	for (size_t i = 0; i < VDIMENSION; i++)
		pVec[i] = rhs.pVec[i];
}

/*
* destructor
* deallocate memory
*/
Vector::~Vector() {
	delete[] pVec;
}

/*
* vector dot product
*/
double operator*(const Vector& a, const Vector& b) {

	return (a.getX() * b.getX() + a.getY() * b.getY() + a.getZ() * b.getZ());
}

/*
* scalar multiplication
*/
Vector operator*(double a, const Vector& b) {
	Vector result;

	result.setX(a * b.getX());
	result.setY(a * b.getY());
	result.setZ(a * b.getZ());

	return result;
}

/*
* scalar multiplication
*/
Vector operator*(const Vector& a, double b) {
	Vector result;

	result.setX(a.getX() * b);
	result.setY(a.getY() * b);
	result.setZ(a.getZ() * b);

	return result;
}

/*
* vector addition
*/
Vector operator+(const Vector& a, const Vector& b) {
	Vector result;

	result.setX(a.getX() + b.getX());
	result.setY(a.getY() + b.getY());
	result.setZ(a.getZ() + b.getZ());

	return result;
}

/*
* scalar addition
*/
Vector operator+(double a, const Vector& b) {
	Vector result;

	result.setX(a + b.getX());
	result.setY(a + b.getY());
	result.setZ(a + b.getZ());

	return result;
}

/*
* scalar addition
*/
Vector operator+(const Vector& a, double b) {
	Vector result;

	result.setX(a.getX() + b);
	result.setY(a.getY() + b);
	result.setZ(a.getZ() + b);

	return result;
}

/*
* vector substraction
*/
Vector operator-(const Vector& a, const Vector& b) {
	Vector result;

	result.setX(a.getX() - b.getX());
	result.setY(a.getY() - b.getY());
	result.setZ(a.getZ() - b.getZ());

	return result;
}

/*
* scalar substraction
*/
Vector operator-(double a, const Vector& b) {
	Vector result;

	result.setX(a - b.getX());
	result.setY(a - b.getY());
	result.setZ(a - b.getZ());

	return result;
}

/*
* scalar substraction
*/
Vector operator-(const Vector& a, double b) {
	Vector result;

	result.setX(a.getX() - b);
	result.setY(a.getY() - b);
	result.setZ(a.getZ() - b);

	return result;
}

/*
* vector unary negation
*/
Vector Vector::operator-() {
	Vector result;

	result.setX(-getX());
	result.setY(-getY());
	result.setZ(-getZ());

	return result;
}

/*
* scalar division
*/
Vector operator/(const Vector& a, double b) {
	Vector result;

	if (b != 0) {
		result.setX(a.getX() / b);
		result.setY(a.getY() / b);
		result.setZ(a.getZ() / b);
	}

	return result;
}

/*
* vector cross product
*/
Vector operator^(const Vector& a, const Vector& b) {
	Vector result;

	result.setX(a.getY() * b.getZ() - a.getZ() * b.getY());
	result.setY(a.getZ() * b.getX() - a.getX() * b.getZ());
	result.setZ(a.getX() * b.getY() - a.getY() * b.getX());

	return result;
}

/*
* vector equality
*/
bool Vector::operator==(const Vector &rhs) {
	return (getX() == rhs.getX() && getY() == rhs.getY() && getZ() == rhs.getZ());
}

/*
* vector less than
*/
bool Vector::operator<(const Vector &rhs) {
	return (magnitude() < rhs.magnitude());
}

/*
* overloaded insertion operator <<
*/
ostream& operator<<(ostream& os, const Vector& v) {
	os << "[ " << v.getX() << ", " << v.getY() << ", " << v.getZ() << " ]";
	return os;
}
Last edited on
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
//main.cpp//
// INCLUDE FILES
#include <iostream>
#include <iomanip>
#include "vector.h"
using namespace std;

int main() {
	// 1. Declare three vector objects A, B, C with the values [1.0,0.0,0.0] , [0.0,1.0,0.0] and [0.0,0.0,1.0] respectively.
	Vector A(1.0, 0.0, 0.0),
		B(0.0, 1.0, 0.0),
		C(0.0, 0.0, 1.0);

	cout.precision(3);
	cout << fixed;
	cout << "1. Three Vectors: A, B, & C." << endl;

	// 2. Display the three vectors.
	cout << "2. A = " << A << " = " << A.magnitude() << endl;
	cout << " B = " << B << " = " << B.magnitude() << endl;
	cout << " C = " << C << " = " << C.magnitude() << endl;

	// 3. Display the dot product of A and B.
	cout << "3. A * B = " << A * B << endl;

	// 4. Display the cross product of A and B.
	Vector crossAB = A ^ B;
	cout << "4. A ^ B = " << crossAB << " = " << crossAB.magnitude() << endl;

	// 5. Display the cross product of B and A (The cross product is not commutative,
	// you should get a different answer than you got for test 4.)
	Vector crossBA = B ^ A;
	cout << "5. B ^ A = " << crossBA << " = " << crossBA.magnitude() << endl;

	// 6. Perform the assignment statement, A = A * 3.27; and then display the value and magnitude of vector, A.
	A = A * 3.27;
	cout << "6. A = " << A << " = " << A.magnitude() << endl;

	// 7. Perform the assignment statement, B = 4.5 + B; and then display the value and magnitude of vector, B.
	B = 4.5 + B;
	cout << "7. B = " << B << " = " << B.magnitude() << endl;

	// 8. Perform the assignment statement, C = C – 1.36; and then display the value and magnitude of vector, C.
	C = C - 1.36;
	cout << "8. C = " << C << " = " << C.magnitude() << endl;

	// 9. Declare vector, D(A – B), and display D.
	Vector D(A - B);
	cout << "9. D = " << D << " = " << D.magnitude() << endl;

	// 10. Display A and B to ensure that their values didn’'t change.
	cout << "10. A = " << A << " = " << A.magnitude() << endl;
	cout << " B = " << B << " = " << B.magnitude() << endl;

	// 11. Declare vector, E(B + C), and display E.
	Vector E(B + C);
	cout << "11. E = " << E << " = " << E.magnitude() << endl;

	// 12. Display B and C to ensure that their values didn’t change.
	cout << "12. B = " << B << " = " << B.magnitude() << endl;
	cout << " C = " << C << " = " << C.magnitude() << endl;

	// 13. Display the dot product of D and E.
	cout << "13. D * E = " << D * E << endl;

	// 14. Display D and E to ensure that their values didn’t change.
	cout << "14. D = " << D << " = " << D.magnitude() << endl;
	cout << " E = " << E << " = " << E.magnitude() << endl;

	// 15. Display the cross product of D and E.
	Vector crossDE = D ^ E;
	cout << "15. D ^ E = " << crossDE << " = " << crossDE.magnitude() << endl;

	// 16. Display D and E to ensure that their values didn’t change.
	cout << "16. D = " << D << " = " << D.magnitude() << endl;
	cout << " E = " << E << " = " << E.magnitude() << endl;

	// 17. Perform the unary negation operator on vector E and then display it
	Vector negE = -E;
	cout << "17. -E = " << negE << " = " << negE.magnitude() << endl;

	// 18. A Unit vector is a vector that points in the same direction as the original vector,
	// but whose magnitude is unity (1). It is calculated by dividing the vector by its magnitude
	// (e.g., VectorA/VectorA.mag()). Calculate and display the Unit vector for E.
	Vector unitE = E / E.magnitude();
	cout << "18. Unit vector of E = " << unitE << " = " << unitE.magnitude() << endl;

	// 19. Display the result (i.e., true or false – NOT 1 or 0) of the operation, A = = B.
	cout << "19. A == B = " << ((A == B) ? "true" : "false") << endl;

	// 20. Display the result (i.e., true or false – NOT 1 or 0) of the operation, D < E.
	cout << "20. D < E = " << ((D < E) ? "true" : "false") << endl;

	// 21. Perform the cascaded assignment A = B = C = Vector(1.5, 2.5, 3.5); and then display all of
	// these vector objects to verify they are indeed equal.
	A = B = C = Vector(1.5, 2.5, 3.5);
	cout << "21. A = " << A << " = " << A.magnitude() << endl;
	cout << " B = " << B << " = " << B.magnitude() << endl;
	cout << " C = " << C << " = " << C.magnitude() << endl;

	return(0);
}
Last edited on
Here are the three errors I get with clang++:
vector.h:75:12: error: unknown type name 'ostream'; did you mean 'std::ostream'?

main.cpp:85:22: error: invalid operands to binary expression ('Vector' and 'double')

vector.h:52:16: error: use of undeclared identifier 'sqrt'

The error in main is because you don't have an operator/ defined for your class.

vector.h line 75 uses ostream, but ostream hasn't been seen yet by the compiler. Reverse lines 3 and 4 in vector.cpp.

main.cpp line 85: You have no division operator defined that takes a vector and a double.

vector.h line 52: sqrt requires the <cmath> header which you have not included.
Hi, thank you for the response.

I revised the error accordingly and now only have a few errors.

I still need to define division operator for vector and double, but not certain about this part what code to input.

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
//vector.h//
// INCLUDE FILES
#ifndef vector_h
#define vector_h
#include <iostream>
#include <cmath>
//
//=============================================================================
// CONSTANT DEFINITION
// none
//=============================================================================
// EXTERNAL CLASS VARIABLE
// none
//=============================================================================
// FUNCTION PROTOTYPES
// none
//=============================================================================
// Class Object
//
class Vector {
	//=============================================================================
public:
	Vector();
	Vector(double x, double y, double z);
	Vector(const Vector&);
	~Vector();

	// getters
	double getX() const {
		return pVec[0];
	}
	double getY() const {
		return pVec[1];
	}
	double getZ() const {
		return pVec[2];
	}

	// setters
	void setX(double x) {
		pVec[0] = x;
	}
	void setY(double y) {
		pVec[1] = y;
	}
	void setZ(double z) {
		pVec[2] = z;
	}

	// return magnitude of vector
	double magnitude() const {
		return sqrt(pVec[0] * pVec[0] + pVec[1] * pVec[1] + pVec[2] * pVec[2]);
	}

	Vector operator-();
	bool operator<(const Vector&);
	bool operator==(const Vector&);
	Vector &operator=(const Vector &rhs);

private:
	const size_t VDIMENSION;
	double *pVec;
	void doCopy(const Vector&);

	friend double operator*(const Vector&, const Vector&);
	friend Vector operator*(const Vector&, double);
	friend Vector operator*(double, const Vector&);
	friend Vector operator+(const Vector&, const Vector&);
	friend Vector operator+(const Vector&, double);
	friend Vector operator+(double, const Vector&);
	friend Vector operator-(const Vector&, const Vector&);
	friend Vector operator-(const Vector&, double);
	friend Vector operator-(double, const Vector&);
	friend Vector operator^(const Vector&, const Vector&);
	friend std::ostream& operator<<(std::ostream&, const Vector&);
};

//=============================================================================
// END OF CONDITIONAL BLOCK
#endif
//=============================================================================
// END OF HEADER FILE
//=============================================================================
keane234 wrote:
I still need to define division operator for vector and double, but not certain about
this part what code to input.

???
Upon closer inspection, you have "operator/" defined in vector.cpp (line 198) but not declared in the header file.
Is that correct way to declare the vector and double in the header file?
I declared in line74 and able to compile fine now.

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
// INCLUDE FILES
#ifndef vector_h
#define vector_h
#include <iostream>
#include <cmath>
//
//=============================================================================
// CONSTANT DEFINITION
// none
//=============================================================================
// EXTERNAL CLASS VARIABLE
// none
//=============================================================================
// FUNCTION PROTOTYPES
// none
//=============================================================================
// Class Object
//
class Vector {
	//=============================================================================
public:
	Vector();
	Vector(double x, double y, double z);
	Vector(const Vector&);
	~Vector();

	// getters
	double getX() const {
		return pVec[0];
	}
	double getY() const {
		return pVec[1];
	}
	double getZ() const {
		return pVec[2];
	}

	// setters
	void setX(double x) {
		pVec[0] = x;
	}
	void setY(double y) {
		pVec[1] = y;
	}
	void setZ(double z) {
		pVec[2] = z;
	}

	// return magnitude of vector
	double magnitude() const {
		return sqrt(pVec[0] * pVec[0] + pVec[1] * pVec[1] + pVec[2] * pVec[2]);
	}

	Vector operator-();
	bool operator<(const Vector&);
	bool operator==(const Vector&);
	Vector &operator=(const Vector &rhs);

private:
	const size_t VDIMENSION;
	double *pVec;
	void doCopy(const Vector&);

	friend double operator*(const Vector&, const Vector&);
	friend Vector operator*(const Vector&, double);
	friend Vector operator*(double, const Vector&);
	friend Vector operator+(const Vector&, const Vector&);
	friend Vector operator+(const Vector&, double);
	friend Vector operator+(double, const Vector&);
	friend Vector operator-(const Vector&, const Vector&);
	friend Vector operator-(const Vector&, double);
	friend Vector operator-(double, const Vector&);
	friend Vector operator^(const Vector&, const Vector&);
	friend Vector operator/(const Vector&, double);
	friend std::ostream& operator<<(std::ostream&, const Vector&);
};

//=============================================================================
// END OF CONDITIONAL BLOCK
#endif
//=============================================================================
// END OF HEADER FILE
//============================================================================= 
Last edited on
Topic archived. No new replies allowed.