OOP Linker Errors?

Hello!
I've just started coding a "Mathematics" library for my own, just to practice some OOP concepts, but sadly I didn't get too far with it before the first errors appeared. That is, I created a Matrix.H and Matrix.CPP file (separate class) in a "Linear Algebra" folder.

However, when I run the code I get the following (linker?) error:

1>------ Build started: Project: Mathematics, Configuration: Debug Win32 ------
1>  Source.cpp
1>Source.obj : error LNK2019: unresolved external symbol "public: __thiscall Matrix<int>::Matrix<int>(int,int)" (??0?$Matrix@H@@QAE@HH@Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "public: int __thiscall Matrix<int>::nrRows(void)" (?nrRows@?$Matrix@H@@QAEHXZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "public: int __thiscall Matrix<int>::nrCols(void)" (?nrCols@?$Matrix@H@@QAEHXZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "public: void __thiscall Matrix<int>::insertRow(int * const,int)" (?insertRow@?$Matrix@H@@QAEXQAHH@Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "public: void __thiscall Matrix<int>::deleteRow(int)" (?deleteRow@?$Matrix@H@@QAEXH@Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "public: void __thiscall Matrix<int>::print(void)" (?print@?$Matrix@H@@QAEXXZ) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "public: __thiscall Matrix<int>::~Matrix<int>(void)" (??1?$Matrix@H@@QAE@XZ) referenced in function _main
1>D:\Programming 2\C++\Mathematics\Debug\Mathematics.exe : fatal error LNK1120: 7 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


The Source.CPP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "Matrix.h"

using namespace std;

int main() {
	Matrix<int> A(7,5);
	int row[] = {5, 10, 9, 11, -5};

	A.insertRow(row, 2);
	A.deleteRow(5);

	cout << "A.nrRows() = " << A.nrRows() << "\n";
	cout << "A.nrCols() = " << A.nrCols() << "\n";

	A.print();

	system("pause");
	return 0;
}


The Matrix.H:

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
#pragma once

using namespace std;

template <class T>
class Matrix {

	private:
		int static const maxSizeMatrix = 1000;
		int numRows;
		int numCols;
		T matrix[maxSizeMatrix][maxSizeMatrix];

	public:
		Matrix(void);
		Matrix(int mNrRows, int mNrCols);
		Matrix(T mMatrix[maxSizeMatrix][maxSizeMatrix], int mNrRows, int mNrCols);
		int nrRows(void);
		int nrCols(void);
		void insertRow(T mRow[], int newRowID);
		void deleteRow(int rowID);
		void insertCol(T mCol[], int newColID);
		void deleteCol(int colID);
		// @TODO: Implement all the other methods
		void print(void);
		~Matrix(void);

};


The Matrix.CPP:

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

using namespace std;

template <class T>
Matrix<T>::Matrix(void) { 
	this.numRows = 0;
	this.numCols = 0;
}

template <class T>
Matrix<T>::Matrix(int mNrRows, int mNrCols) {
	this.numRows = mNrRows;
	this.numCols = mNrCols;
	for (int i = 1; i <= this.numRows; ++i) {
		for (int j = 1; j <= this.numCols; ++j) {
			this.matrix[i][j] = 0;
		}
	}
}

template <class T>
Matrix<T>::Matrix(T mMatrix[maxSizeMatrix][maxSizeMatrix], int mNrRows, int mNrCols) {
	this.matrix = mMatrix;
	this.numRows = mNrRows;
	this.numCols = mNrCols;
}

template <class T>
int Matrix<T>::nrRows(void) {
	return (this.numRows);
}

template <class T>
int Matrix<T>::nrCols(void) {
	return (this.numCols);
}

template <class T>
void Matrix<T>::insertRow(T mRow[], int newRowID) {
	this.numRows++;
	for (int i = this.numRows; i > newRowID; --i) {
		for (int j = 1; j <= this.numCols; ++j) {
			this.matrix[i][j] = this.matrix[i-1][j];
		}
	}
	for (int i = 1; i <= this.numCols; ++i) {
		this.matrix[newRowID][i] = mRow[i];
	}
}

template <class T>
void Matrix<T>::deleteRow(int rowID) {
	for (int i = rowID+1; i <= this.numRows; ++i) {
		for (int j = 1; j <= this.numCols; ++j) {
			this.matrix[i-1][j] = this.matrix[i][j];
		}
	}
	this.numRows--;
}

// @TODO: Add insert/delete methods for Columns

template <class T>
void Matrix<T>::print(void) {
	for (int i = 1; i <= this.numRows; ++i) {
		cout << "( ";
		for (int j = 1; j <= this.numCols; ++j) {
			cout << setw(4) << this.matrix[i][j] << " ";
		}
		cout << ")" << "\n";
	}
}

template <class T>
Matrix<T>::~Matrix(void) { }


PS: I am using Visual Studio 2012 (stating this in case it could be related to the reason for which I get the error).

Could you please help me figure out why these errors appear and how to fix them?
Thanks in advance!
You can't separate the templated class into a header and source file.

For more info:
http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
Last edited on
Oh.. That makes sense.. Sorry for wasting your time, and thank you very much for clarifying this :)

Raul
Topic archived. No new replies allowed.