Matix.h
#ifndef _MATRIX_H_#define _MATRIX_H_#include <stdint.h>#include <vector>template <class T> class Matrix{ public: Matrix(uint64_t unNumRows, uint64_t unNumCols, long double dbDefaultElement/* = 0.0*/); ~Matrix(); std::vector<std::vector<long double>> GetCore(); void SetCore(std::vector<std::vector<long double>> core); void GetDimensions(uint64_t & unNumRows, uint64_t & unNumCols); std::vector<uint8_t> GetDimensions(); uint64_t GetRowSize(); uint64_t GetColSize(); void SetElement(long double dbElement, uint64_t unRow, uint64_t unCol); long double GetElement(uint64_t unRow, uint64_t unCol); Matrix operator=(Matrix & rhs); Matrix operator+(Matrix & rhs); Matrix operator-(Matrix & rhs); Matrix operator*(Matrix & rhs); static Matrix Transpose(Matrix & matrix); protected: std::vector<std::vector<long double>> TheMatrix;};#endif
Matrix.cpp
#include "Matrix.h"template <class T>Matrix<T>::Matrix(uint64_t unNumRows, uint64_t unNumCols, long double dbDefaultElement/* = 0.0*/){ for (uint64_t c=0; c<unNumCols; c++) { std::vector<long double> NewCol; for (uint64_t r=0; r<unNumRows; r++) { NewCol.push_back(dbDefaultElement); } TheMatrix.push_back(NewCol); }}template <class T>Matrix<T>::~Matrix(){}template <class T>std::vector<std::vector<long double>> Matrix<T>::GetCore(){ return TheMatrix;}template <class T>void Matrix<T>::SetCore(std::vector<std::vector<long double>> core){ TheMatrix = core;}template <class T>Matrix<T> Matrix<T>::operator=(Matrix & rhs){ TheMatrix = rhs.GetCore(); return *this;}template <class T>Matrix<T> Matrix<T>::operator+(Matrix<T> & rhs){ if (rhs.GetDimensions() != this->GetDimensions()) { Matrix<T> NullMatrix(0, 0); return NullMatrix; } uint64_t unRows, unCols; GetDimensions(unRows, unCols); Matrix<T> ResultMatrix(unRows, unCols); for (uint64_t j=0; j<unCols; j++) { for (uint64_t i=0; i<unRows; i++) { ResultMatrix.SetElement(this->GetElement(i, j) + rhs.GetElement(i, j), i, j); } } return ResultMatrix;}template <class T>Matrix<T> Matrix<T>::operator-(Matrix & rhs){ if (rhs.GetDimensions() != this->GetDimensions()) { Matrix NullMatrix(0, 0); return NullMatrix; } uint64_t unRows, unCols; GetDimensions(unRows, unCols); Matrix<T> ResultMatrix(unRows, unCols); for (uint64_t j=0; j<unCols; j++) { for (uint64_t i=0; i<unRows; i++) { ResultMatrix.SetElement(this->GetElement(i, j) - rhs.GetElement(i, j), i, j); } } return ResultMatrix;}template <class T>Matrix<T> Matrix<T>::operator*(Matrix<T> & rhs){ uint64_t unRowsLeft, unColsLeft, unRowsRight, unColsRight; rhs.GetDimensions(unRowsRight, unColsRight); this->GetDimensions(unRowsLeft, unColsLeft); if (unColsLeft != unRowsRight) { Matrix<T> NullMatrix(0, 0); return NullMatrix; } Matrix<T> ResultMatrix(unRowsLeft, unColsRight); long double dbSum; for (uint64_t i=0; i<unRowsLeft; i++) { for (uint64_t j=0; j<unColsRight; j++) { dbSum = 0.0; for (uint64_t k=0; k<unRowsRight; k++) // or unColsLeft, they are same { dbSum += GetElement(i, k) * rhs.GetElement(k, j); } ResultMatrix.SetElement(dbSum, i, j); } } return ResultMatrix;}template <class T>void Matrix<T>::GetDimensions(uint64_t & unNumRows, uint64_t & unNumCols){ unNumCols = TheMatrix.size(); if (unNumCols == 0) { unNumRows = 0; } else { unNumRows = TheMatrix.at(0).size(); }}template <class T>uint64_t Matrix<T>::GetRowSize(){ if (GetColSize() == 0) return 0; return static_cast<uint64_t>(TheMatrix.at(0).size());}template <class T>uint64_t Matrix<T>::GetColSize(){ return static_cast<uint64_t>(TheMatrix.size());}template <class T>std::vector<uint8_t> Matrix<T>::GetDimensions(){ uint64_t unNumRows, unNumCols; GetDimensions(unNumRows, unNumCols); std::vector<uint8_t> Dimensions; Dimensions.push_back(unNumRows); Dimensions.push_back(unNumCols); return Dimensions;}template <class T>void Matrix<T>::SetElement(long double dbElement, uint64_t unRow, uint64_t unCol){ if ((unRow >= GetRowSize()) || (unCol >= GetColSize())) return; TheMatrix.at(unCol).at(unRow) = dbElement;}template <class T>long double Matrix<T>::GetElement(uint64_t unRow, uint64_t unCol){ if (unRow >= GetRowSize()) return 0.0; if (unCol >= GetColSize()) return 0.0; return TheMatrix.at(unCol).at(unRow);}template <class T>Matrix<T> Matrix<T>::Transpose(Matrix & matrix){ uint64_t rows, cols; matrix.GetDimensions(rows, cols); Matrix TransposedMatrix(cols, rows); for (uint64_t c=0; c<rows; c++) { for (uint64_t r=0; r<cols; r++) { TransposedMatrix.SetElement(matrix.GetElement(r, c), c, r); } } return TransposedMatrix;}
main.cpp
#include <iostream>#include "Matrix.h"int wmain(int argc, wchar_t * argv[]){ Matrix<long double> mat1(3, 3, 0.0); mat1.SetElement(11, 0, 0); system("pause"); return 0;}
Error messages:
Quote:Error 4 Math C:\Development\IDE\Visual Studio 2010\SAVE\Grand Solution\x64\Debug\Math.exe 1 error LNK1120: 3 unresolved externals
Error 1 Math C:\Development\IDE\Visual Studio 2010\SAVE\Grand Solution\Math\main.obj error LNK2019: unresolved external symbol "public: __cdecl Matrix<long double>::~Matrix<long double>(void)" (??1?$Matrix@O@@QEAA@XZ) referenced in function wmain
Error 3 Math C:\Development\IDE\Visual Studio 2010\SAVE\Grand Solution\Math\main.obj error LNK2019: unresolved external symbol "public: __cdecl Matrix<long double>::Matrix<long double>(unsigned __int64,unsigned __int64,long double)" (??0?$Matrix@O@@QEAA@_K0O@Z) referenced in function wmain
Error 2 Math C:\Development\IDE\Visual Studio 2010\SAVE\Grand Solution\Math\main.obj error LNK2019: unresolved external symbol "public: void __cdecl Matrix<long double>::SetElement(long double,unsigned __int64,unsigned __int64)" (?SetElement@?$Matrix@O@@QEAAXO_K0@Z) referenced in function wmain
Could anyone please tell me why my code gives these linker errors, and how do I get rid of them?