Linking Error 2019

I am getting this error message when I compile my code.


Build Output Message:

1>------ Build started: Project: matrixWK06, Configuration: Debug Win32 ------
1>Linking...
1>matrix2Test.obj : error LNK2019: unresolved external symbol "class Matrix __cdecl operator+(class Matrix const &,class Matrix const &)" (??H@YA?AVMatrix@@ABV0@0@Z) referenced in function _main
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>C:\ccclibGraphicTest\ccclibGraphicTest\matrixWK06\Debug\matrixWK06.exe : fatal error LNK1120: 2 unresolved externals
1>Build log was saved at "file://c:\ccclibGraphicTest\ccclibGraphicTest\matrixWK06\matrixWK06\Debug\BuildLog.htm"
1>matrixWK06 - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Did you declare some "operator+(Matrix,Matrix)" in some header and forgot to implement it in the C++ file? If so, just implement the operator.

Or the Matrix class isn't your class and you forgot to link to a library which declares the Matrix operator? Then add the library to the linker's "Additional Libraries" in the properties section of the project.

Ciao, Imi
try the overloading operator '+' declared as friend function .
Here is the code for the header file. Can you tell me what I am missing?

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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#ifndef MATRIX2_H
#define MATRIX2_H	

#include <iostream>
#include <cassert>

using namespace std;

/**
	This class describes a row in a matrix.
*/
class Matrix;
class MatrixRow
{
public:
	/**
		Remembers a row for a given matrix.
		@param m a pointer to the matrix
		@param s the size of the row
	*/
	MatrixRow(Matrix* m, int s);

	/**
		Accesses a row element.
		@param j the column index
		@return a reference to the element with the given index
	*/
	double& operator[](int j);

private:
	Matrix* mat;
	int i;
};

/**
	This class describes a row in a constant matrix.
*/
class ConstMatrixRow
{
public:
	/**
		Constructs a row with a given start and size.
		@param m a pointer to the matrix
		@param s the size of the row
	*/
	
	ConstMatrixRow(const Matrix* m, int s);

	/**
		Accesses a row element.
		@param j the column index
		@return a reference to the element with the given index
	*/
	double operator[](int j) const;

private:
		const Matrix* mat;
		int i;
};

/**
	This class describes a matrix with arbitrary rows and columns.
*/
class Matrix
{
public:
		/**
			Constructs a matrix filled with zero elements.
		*/
		Matrix(int r, int c);

		/**
			The big three-copy constructor, assignment operator, and destructor.
		*/
		Matrix(const Matrix& other);
		Matrix& operator=(const Matrix& other);
		~Matrix();

		/**
			Gets the number of rows of this matrix.
			@return the number of rows
		*/
		int get_rows() const;

		/**
			Gets the number of columns of this matrix.
			@return the number of columns
		*/
		int get_columns() const;

		/** (NEW - wk06 assignment)
			Negates a matrix.
			@returns the negated matrix.
		*/
		Matrix operator~();

/**
	Accesses a matrix element.
	@param i the row index
	@param j the column index
	@return a reference to the element with the given indexes
*/
double& operator()(int i, int j);

/**
	Accesses a matrix element
	@param i the row index
	@param j the column index
	@return the element with the given indexes
*/
double operator()(int i, int j) const;

/**
	Accesses a matrix row.
	@param i the row index
	@return the row with the given index
*/
MatrixRow operator[](int i);

/**
	Accesses a matrix row.
	@param i the row index
	@return the row with the given index
*/
ConstMatrixRow operator[](int i) const;

/**
	Computes the matrix sum.
	@param right another matrix
	@return the updated matrix
*/
Matrix& operator+=(const Matrix& right);

/** (NEW - wk06 assignment)
	Computes the compound operation of
	matrix subtraction and assignment.
	@param right another matrix
	@return the updated matrix
*/
Matrix operator-=(const Matrix& right);

private:
	/**
		Copies another matrix into this matrix.
		@param other the other matrix
	*/
	void copy(const Matrix& other);

	/**
		Frees the memory for this matrix.
	*/
	void free();

	int rows;
	int columns;
	double* elements;
};

/**(NEW - wk06 assignment)
	Computes the matrix difference (1st implementation).
	@param right another matrix
	@return the difference of two matrices
*/
Matrix operator-(const Matrix& left, const Matrix& right);

/** (NEW - wk06 assignment)
	Computes the matrix difference (2nd implementation).
	@param right another matrix
	@return the difference of two matrices
*/
Matrix operator-(const Matrix& left, Matrix& right);

/**
	Computes the matrix sum.
	@param right another matrix
	@return the sum of two matrices
*/
Matrix operator+(const Matrix& left, const Matrix& right);

/**
	Computes the matrix product.
	@param right another matrix
	@return the product of two matrices
*/
Matrix operator*(const Matrix& left, const Matrix& right);

/** (NEW - wk06 assignment)
	Computes the sum of a scalar value and a matrix.
	@param left a scalar value
	@param right a matrix
	@return the sum of the given value and the given matrix
*/
Matrix operator+(double left, const Matrix& right);

/** (NEW - wk06 assignment)
	Computes the difference of a scalar value and a matrix
	@param left a scalar value
	@param right a matrix
	@return the difference of the given value and the given matrix
*/
Matrix operator-(double left, const Matrix& right);

/**
	Computes the scalar product of a scalar value and a matrix.
	@param left a scalar value
	@param right a matrix
	@return the product of the given value and the given matrix
*/
Matrix operator*(double left, const Matrix& right);

/**
	Computes the sum of a matrix and a scalar value.
	@param right a scalar value
	@return the sum this matrix and the given value
*/
Matrix operator+(const Matrix& left, double right);

/**
	Computes the substraction of a matrix and a scalar value.
	@param right a scalar value
	@return the substraction this matrix and the given value
*/
Matrix operator-(const Matrix& left, double right);

/**
	Computes the scalar product of a matrix and a scalar value.
	@param right a scalar value
	@return the product of this matrix and the given value
*/
Matrix operator*(const Matrix& left, double right);

/**
	Prints a matrix to an output stream.
	@param left an output stream
	@param right a matrix
	@return the given output stream
*/
ostream& operator<<(ostream& left, const Matrix& right);

inline Matrix::Matrix(const Matrix& other)
{
	copy(other);
}

inline Matrix::~Matrix()
{
	free();
}

inline int Matrix::get_rows() const
{
	return rows;
}

inline int Matrix::get_columns() const
{
	return columns;
}

inline void Matrix::free()
{
	delete[] elements;
}

inline double& Matrix::operator()(int i, int j)
{
	assert(0 <= i && i < rows && 0 <= j && j < columns);
	return elements[i * get_columns() + j];
}

inline double Matrix::operator()(int i, int j) const
{
	assert(0 <= i && i < rows && 0 <= j && j < columns);
	return elements[i * get_columns() + j];
}

inline MatrixRow Matrix::operator[](int i)
{
	return MatrixRow(this, i);
}

inline ConstMatrixRow Matrix::operator[](int i) const
{
	return ConstMatrixRow(this, i);
}

inline MatrixRow::MatrixRow(Matrix* m, int s) : mat(m), i(s) { }

inline double& MatrixRow::operator[](int j)
{
	return (*mat)(i,j);
}

inline ConstMatrixRow::ConstMatrixRow(const Matrix* m, int s)
: mat(m), i(s) { }

inline double ConstMatrixRow::operator[](int j) const
{
	return (*mat)(i, j);
}

inline Matrix operator*(double left, const Matrix& right)
{
	return right * left;
}

#endif 
Here is the code for the source file. Can you tell me what I am missing?

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
#include <iomanip>
#include "matrix2.h"

Matrix::Matrix(int r, int c)
	: rows(r), columns(c), elements(new double[rows * columns])
{
	for (int i = 0; i < rows * columns; i++)
		elements[i] = 0;
}

Matrix& Matrix::operator=(const Matrix& other)
{
	if (this != &other)
	{
		free();
		copy(other);
	}
	return *this;
}

void Matrix::copy(const Matrix& other)
{
	rows = other.rows;
	columns = other.columns;
	elements = new double[rows * columns];
	for (int i = 0; i < rows * columns; i++)
		elements[i] = other.elements[i];
}

Matrix& Matrix::operator+=(const Matrix& right)
{
	assert(rows == right.rows && columns == right.columns);
	for (int i = 0; i < rows; i++)
		for (int j = 0; j < columns; j++)
			(*this)(i, j) += right(i, j);
	return *this;
}

Matrix operator+(const Matrix& left, const Matrix& right)
{
	Matrix result = left;
	result += right;
	return result;
}

Matrix operator*(const Matrix& left, const Matrix& right)
{
	assert(left.get_columns() == right.get_rows());
	Matrix result(left.get_rows(), right.get_columns());
	for (int i = 0; i < left.get_rows(); i++)
		for (int j = 0; j < right.get_columns(); j++)
			for (int k = 0; k < left.get_columns(); k++)
				result(i, j) += left(i, k) * right(k, j);
	return result;
}

Matrix operator*(const Matrix& left, double right)
{
	Matrix result(left);
	for (int i = 0; i < result.get_rows(); i++)
		for (int j = 0; j < result.get_columns(); j++)
			result(i, j) *= right;
	return result;
}

ostream& operator<<(ostream& left, const Matrix& right)
{
	const int WIDTH = 10;
	for (int i = 0; i < right.get_rows(); i++)
	{
		for (int j = 0; j < right.get_columns(); j++)
			left << setw(WIDTH) << right(i, j);
		left << "\n";
	}
	return left;
}
nope, sorry. Can't tell you. Everything looks fine to me.

Maybe except... did you forget to add the cpp file to your project?

Ciao, Imi.
The second code I submitted is the cpp file. I also created a new project thinking that the original was was created as a console file. I ran the new project and still get the 2019 and 1120 link errors.
I was able to resolve my problem. I had created the code as a WIN32 Project when I should have created it as a WIN32 Console Application. I tried this and it worked. Thanks to all who had offered their advise.
Topic archived. No new replies allowed.