2016-04-06 23 views
0

için şablon bağımsız değişkenini belirleyemedi Nesne yönelimli programlama ve C++ için çok yeni. Bir matris sınıfı ve squarematrix sınıfı üzerinde çalışıyorum ve anlamayacağımı düşündüğüm bazı problemlerle karşılaşıyorum. Ben elde edilmiştir hata kodudur:C++ Hata Kodu C2784,

C2784: 
'matrix<T,m,k> operator *(matrix<T,m,n> &,matrix<T,n,k> &)': could not 
deduce template argument for 'matrix<T,m,n> &' from 
'std::vector<std::vector<double,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>>' 

Gerçekten emin değilim benim kod işin diğer bölümlerini oldu çünkü neden. Hata "product.elements = product.elements * elements;" ile satırda raporlanır.

//Source.cpp 
#include"Squarematrix.h" 
#include<iostream> 
#include<vector> 
using namespace std; 
int main() { 
    vector<double> a = { 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; 
for (int i = 0; i < m; i++) { 
    for (int j = 0; j < k; j++) { 
     for (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) { 
    for (int i = 0; i < m; ++i) { 
    for (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; 
    for (int power = 2; power <= k; power++) { 
    product.elements = product.elements * elements; 
    } 
    return product; 
} 
#endif _Squarematrix_ 
+0

'_Matrix_', bir alt harfi ve ardından büyük harfle takip ettiği için UB'nin bir kamyon yükünü tanıtır. ** Bunu yapmayın, aksi takdirde derleyici kedinizi yiyebilir. – Bathsheba

+0

Tavsiye için teşekkürler. Ancak biraz daha detaylandırır mısınız? Bu neden sorun? UB Nedir? – Bnakey

cevap

1

Sen nrow ve ncol gerekmez - onlar şablon parametreleri konum ve derleme zamanında da bilinir.

Ama bu sorun değil - Eğer squarematrix çarparak olmalıdır nerede std::vector çarparak ediyoruz: Bir birim matris oluşturan bir hayali fonksiyonunu kullandım

template<class T, int n> 
squarematrix<T, n> squarematrix<T, n>::pow(int k){ 
    squarematrix<T, n> product = unit_matrix<T, n>(); 
    for (int power = 1; power <= k; power++) { 
     product = product * *this; 
    } 
    return product; 
} 

.
Bu işlevi bir egzersiz olarak bırakmak.

+0

Orijinal olarak, bu sorunu atlayacağını düşündüğüm squarematrix sınıfı için bir kopya oluşturucu oluşturdum. (Daha önce kodumun geri kalanıyla paylaşmayı unuttum - benim kötü). Şimdi orada. Yine de aynı hatayı alıyorum. Sanırım şimdi anlamadığım şey: ürünü tanımlamak için bir kopya kurucusu kullandım, ancak bir vektör yaratıyor. Niye ya? – Bnakey

+0

@Bnakey Bir kopya oluşturucu, 'product.elements = product.elements * elements; 'squarematrix' içermez. – molbdnilo

1

bu kod product.elements = product.elements * elements Eğer çoğalmaya iki std::vector kullanmak istediğiniz ifade, ancak kimin tip std::vector olan iki parametre ile operator * operasyonu desteklemez. Kodunuzda , sen türü matrisi ile operator * operasyona destek, böylece kullanmak isterseniz, OK olacak product.elements = (product * *this).elements

kodu product.elements = product.elements * elements değişmelidir. yüzden sınıfa Squarematrix üyesi fonksiyonu pow kodudur:

sonunda
template<class T, int n> 
squarematrix<T, n> squarematrix<T, n>::pow(int k){ 
    squarematrix<T, n> product; 
    product.elements = this->elements; 
    for (int power = 2; power <= k; power++) { 
     product.elements = (product * *this).elements; 
    }  
    return product; 
}   

, #endif aksi halde derleyici bazı uyarı ve hataları fırlatır, bazı önceden tanımlanmış sonudur ve bazı makro izlemez.