i new object oriented programming , c++. have been working on matrix class , squarematrix class , have been running problems can't seem figure out. error code have been getting is:
c2784: 'matrix<t,m,k> operator *(matrix<t,m,n> &,matrix<t,n,k> &)': not deduce template argument 'matrix<t,m,n> &' 'std::vector<std::vector<double,std::allocator<_ty>>,std::allocator<std::vector<_ty,std::allocator<_ty>>>>'
i unsure why, because have had other parts of code work. error reported in line "product.elements = product.elements * elements;"
//source.cpp #include"squarematrix.h" #include<iostream> #include<vector> using namespace std; int main() { vector<double> = { 1, 2,4,5,6}; squarematrix<double,2> n; n.assign(a); cout << n << n.pow(2)<< endl; return(0); } //matrix.h #ifndef _matrix_ #define _matrix_ #include <iostream> #include <vector> #include <math.h> using namespace std; template<class t, int m, int n> class matrix { public: vector<vector<t>> elements; int nrow; int ncol; matrix(); matrix(matrix<t, m, n>&); }; template<class t, int m, int n> matrix<t, m, n>::matrix() { vector<t>temp(n, 0); elements.assign(m, temp); nrow = m; //m=0 ncol = n; //n=0 } template<class t, int m, int n> matrix<t, m, n>::matrix(matrix<t, m, n>& a) { elements = a.elements; nrow = m; ncol = n; } template<class t, int m, int n, int k> matrix<t, m, k> operator*(const matrix<t, m, n>& a, const matrix<t, n, k>& b) { matrix<t, m, k> product; (int = 0; < m; i++) { (int j = 0; j < k; j++) { (int h = 0; h < n; h++) product.elements[i][j] += a.elements[i][h] * b.elements[h][j]; } } return product; } template<class t, int m, int n> ostream& operator<< (ostream& o, const matrix<t, m, n>& input) { (int = 0; < m; ++i) { (int j = 0; j < n; ++j) o << input.elements[i][j] << " "; o << endl; } return o; } #endif _matrix_ //squarematrix.h #ifndef _squarematrix_ #define _squarematrix_ #include "matrix.h" #include <iostream> #include <vector> using namespace std; template<class t, int n> class squarematrix : public matrix<t, n, n> { public: squarematrix(); squarematrix(squarematrix<t, n>&); squarematrix<t, n> pow(int); //calculate a^k }; template<class t, int n> squarematrix<t, n>::squarematrix(){ vector<t>temp(n, 0); elements.assign(n, temp); nrow = n; //n=0 ncol = n; //n=0 } template<class t, int n> squarematrix<t, n>::squarematrix(squarematrix<t, n>& a){ elements = a.elements; nrow = n; ncol = n; } template<class t, int n> squarematrix<t, n> squarematrix<t, n>::pow(int k){ squarematrix<t, n> product; product.elements = elements; (int power = 2; power <= k; power++) { product.elements = product.elements * elements; } return product; } #endif _squarematrix_
you don't need nrow
, ncol
- they're template parameters , known @ compile time.
but that's not problem - you're multiplying std::vector
should multiplying squarematrix
:
template<class t, int n> squarematrix<t, n> squarematrix<t, n>::pow(int k){ squarematrix<t, n> product = unit_matrix<t, n>(); (int power = 1; power <= k; power++) { product = product * *this; } return product; }
where i've used fictitious function creates unit matrix.
writing function left exercise.
Comments
Post a Comment