Trying to write a template class for a matrix object. Getting compiling errors:
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(606): error C2558: class 'Matrix' : no copy constructor available or copy constructor is declared 'explicit' 1> with 1> [ 1> T=float 1> ] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(605) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' 1>
with 1> [ 1> _Ty=Matrix 1> ] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(751) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' being compiled 1> with 1> [ 1>
_Ty=Matrix 1> ] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\type_traits(743) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled 1> with 1> [ 1>
_Ty=Matrix 1> ] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled 1> with 1> [ 1>
_Ty=std::allocator> 1> ] 1> e:\projects\work\nns\fifteenstepstut\fifteensteps\fifteensteps\source.cpp(35) : see reference to class template instantiation 'std::vector<_Ty>' being compiled 1> with 1> [ 1>
_Ty=Matrix 1> ] 1> 1>Build FAILED. 1>
AND
ClCompile: 1> Source.cpp 1>e:\projects\work\nns\fifteenstepstut\fifteensteps\fifteensteps\matrix.h(80): error C2664: 'Matrix::Matrix(Matrix &)' : cannot convert parameter 1 from 'Matrix (__cdecl *)(void)' to 'Matrix &' 1>
with 1> [ 1> T=float 1> ] 1>
e:\projects\work\nns\fifteenstepstut\fifteensteps\fifteensteps\matrix.h(74) : while compiling class template member function 'Matrix Matrix::dot(const Matrix &)' 1> with 1> [ 1>
T=float 1> ] 1>
e:\projects\work\nns\fifteenstepstut\fifteensteps\fifteensteps\source.cpp(62) : see reference to function template instantiation 'Matrix Matrix::dot(const Matrix &)' being compiled 1> with 1> [ 1> T=float 1> ] 1>
e:\projects\work\nns\fifteenstepstut\fifteensteps\fifteensteps\source.cpp(38) : see reference to class template instantiation 'Matrix' being compiled 1> with 1> [ 1> T=float 1>
] 1>e:\projects\work\nns\fifteenstepstut\fifteensteps\fifteensteps\matrix.h(84): error C2664: 'Matrix::Matrix(Matrix &)' : cannot convert parameter 1 from 'Matrix (__cdecl *)(void)' to 'Matrix &' 1>
with 1> [ 1> T=float 1> ] 1> 1>Build FAILED.
"Matrix.h"
#include <vector>
#include <iostream>
template<typename T>
class Matrix {
private:
std::vector<T> data;
int rows;
int cols;
public:
Matrix();
Matrix(std::vector<T>, int rows, int cols);
Matrix(Matrix<T>&); //change with this one
//Matrix(const Matrix<T>&); //Will need to uncomment to test the 3rd error
void print();
Matrix<T> transpose();
Matrix<T> dot(const Matrix<typename std::remove_reference<T>::type> &); //error 2
//Matrix<T&> dot(const Matrix<T> &); //dumb idea?
//Matrix<T> dot(const Matrix<T> &); //error 1
};
template <typename T>
Matrix<T>::Matrix() {
data.clear();
rows = 0;
cols = 0;
}
template <typename T>
Matrix<T>::Matrix(std::vector<T> elements, int numRows, int numCols) {
rows = numRows;
cols = numCols;
data.clear();
for(unsigned int i = 0; i < elements.size(); i++) {
data.push_back(elements[i]);
}
}
template <typename T>
Matrix<T>::Matrix(Matrix<T>& matrix) {
rows = matrix.rows;
cols = matrix.cols;
data.clear();
for(unsigned int i = 0; i < matrix.data.size(); i++) {
data.push_back(matrix.data[i]);
}
}
/* To get compiler error, exchange with a above
template <typename T>
Matrix<T>::Matrix(const Matrix<T>& matrix) {
rows = matrix.rows;
cols = matrix.cols;
data.clear();
for(unsigned int i = 0; i < matrix.data.size(); i++) {
data.push_back(matrix.data[i]);
}
}*/
template <typename T>
Matrix<T> Matrix<T>::dot(const Matrix<typename std::remove_reference<T>::type> & rhs) { //ERROR 2
//Matrix<&T> dot(const Matrix<T> &) {
//Matrix<T> dot(const Matrix<T> &) { ERROR 1
if(cols != rhs.rows) {
std::cout << "Error! Can not resolve dot product on these matrices!" << std::endl;
std::cout << "Requested: [" << rows << "x" << cols << "] <alt+7> [" << rhs.rows << "x" << rhs.cols << "]" << std::endl;
Matrix<T> matrix();
return matrix;
}
Matrix<T> matrix();
return matrix;
}
template <typename T>
void Matrix<T>::print() {
for(unsigned int i = 0; i < data.size(); i++) {
std::cout << data[i] << ", ";
if((i+1) % cols == 0)
std::cout << std::endl;
}
}
template <typename T>
Matrix<T> Matrix<T>::transpose() {
std::vector<T> vec;
for(unsigned int i = 0; i < data.size(); i++) {
vec.push_back(data[(cols*(i%rows)+i/rows)]);
}
return Matrix<T>(vec, cols, rows);
}
I've read several different ideas on how to correct this, but not really sure what the issue is. A lot of places talk about passing just T as a const reference, but in this case, I'm passing the class as a const reference. Does not seem to like that.
I finally decided to see what happens if a implement a const reference copy constructor.
I then get this error:
unresolved external symbol "public: class Matrix __thiscall Matrix::dot(class Matrix const &)" (?dot@?$Matrix@M@@QAE?AV1@ABV1@@Z) referenced in function "void __cdecl testMatrixClass(void)" (?testMatrixClass@@YAXXZ)
How could I accomplish passing this class as a const reference if at all possible?
Thanks!
Test implementation
SOURCE.CPP
#include <iostream>
#include <vector>
#include "Matrix.h"
#include <string>
#include <fstream>
#include <sstream>
////TODO: Find alternatives to these...
//typedef std::vector<std::vector<float>> Matrix;
//typedef std::vector<float> Vector;
//using LMath::operator+;
//using LMath::operator==;
//void testMatrix(); //testing function.
//Matrix loadData(std::string); //Not implemented yet
//bool saveData(Matrix, std::string); //Not implemented yet
void testMatrixClass();
int main() {
//testMatrix();
testMatrixClass();
return 0;
}
void testMatrixClass() {
std::vector<Matrix<float>> testResults;
std::vector<std::string> testInfo;
Matrix<float> temp;
testResults.push_back(temp);
testInfo.push_back("Default Constructor");
std::vector<float> tempVec;
for(int i = 0; i < 9; i++) {
tempVec.push_back((float)(i%3));
}
Matrix<float> temp2(tempVec, 3, 3);
testResults.push_back(temp2);
testInfo.push_back("Vector constructor");
testResults.push_back(temp2.transpose());
testInfo.push_back("Vector transpose");
tempVec.push_back(10.0);
Matrix<float> temp3(tempVec, 5, 2);
testResults.push_back(temp3);
testInfo.push_back("Vector constructor");
testResults.push_back(temp3.transpose());
testInfo.push_back("Vector transpose");
testResults.push_back(temp2.dot(temp2));
testInfo.push_back("Dot product");
testResults.push_back(temp2.dot(temp3));
testInfo.push_back("Error Dot Product");
for(unsigned int i = 0; i < testResults.size(); i++) {
std::cout << "Test: " << testInfo[i] << ": " << std::endl;;
testResults[i].print();
std::cout << std::endl;
}
}
Solution:
#include <iostream>
#include <vector>
template<typename T>
class Matrix {
private:
std::vector<T> data;
int rows;
int cols;
public:
Matrix();
Matrix(std::vector<T>, int rows, int cols);
//Matrix(Matrix<T>&);
Matrix(const Matrix<T>&);
void print();
Matrix<T> transpose();
Matrix<T> dot(const Matrix<typename std::remove_reference<T>::type> &);
};
template <typename T>
Matrix<T>::Matrix() {
data.clear();
rows = 0;
cols = 0;
}
template <typename T>
Matrix<T>::Matrix(std::vector<T> elements, int numRows, int numCols) {
rows = numRows;
cols = numCols;
data.clear();
for(unsigned int i = 0; i < elements.size(); i++) {
data.push_back(elements[i]);
}
}
template <typename T>
Matrix<T>::Matrix(const Matrix<T>& matrix) {
rows = matrix.rows;
cols = matrix.cols;
data.clear();
for(unsigned int i = 0; i < matrix.data.size(); i++) {
data.push_back(matrix.data[i]);
}
}
template <typename T>
void Matrix<T>::print() {
for(unsigned int i = 0; i < data.size(); i++) {
std::cout << data[i] << ", ";
if((i+1) % cols == 0)
std::cout << std::endl;
}
}
template <typename T>
Matrix<T> Matrix<T>::transpose() {
std::vector<T> vec;
for(unsigned int i = 0; i < data.size(); i++) {
vec.push_back(data[(cols*(i%rows)+i/rows)]);
}
return Matrix<T>(vec, cols, rows);
}
template <typename T>
Matrix<T> Matrix<T>::dot(const Matrix<typename std::remove_reference<T>::type> & rhs) {
if(cols != rhs.rows) {
std::cout << "Error! Can not resolve dot product on these matrices!" << std::endl;
std::cout << "Requested: [" << rows << "x" << cols << "] <alt+7> [" << rhs.rows << "x" << rhs.cols << "]" << std::endl;
Matrix<T> matrix;
return matrix;
}
Matrix<T> matrix;
return matrix;
}
Matrix<T>
in several places; that requires a copy method. Try returning aconst Matrix<T>
– AganjuMatrix
member is astd::vector
, andstd::vector
is Rule of Five complaint.Matrix
should be able to observe the Rule of Zero. – user4581301