2016-04-01 41 views
0

テンプレートを使用して行列行列乗算を実行しようとしていますが、次のエラーが発生し続けます。 (私は非正方行列を乗算しようとしています)テンプレート行列 - 行列乗算C++

エラー1つのエラーC2593:「演算子*は」

あいまいであるいずれかが私にこの問題を解決する方法についてのアドバイスを与えることができますか?

//Matrix.h 
#pragma once 
#include <iostream> 
#include <vector> 
using namespace std; 

template<class T, int m, int n> 
class Matrix; 

template<class T, int m, int n, int l> 
Matrix<T, m, n> operator*(const Matrix<T, m, n>&, const Matrix<T, n, l>&); 

template<class T, int m, int n> 
class Matrix 
{ 
vector<vector<T>> elements; 
int nrow; 
int ncol; 

public: 
    Matrix(); 
    ~Matrix(); 
    void print(); 
    template<int l> 
    friend Matrix<T, m, l> operator*<>(const Matrix<T, m, n>&, const Matrix<T, n, l>&); 

}; 

template<class T, int m, int n> 
Matrix<T, m, n>::Matrix() : nrow(m), ncol(n) 
{ 
    for (int i = 0; i < nrow; i++){ 
     vector<T> row(ncol, i); 
     elements.push_back(row); 
    } 
} 

template<class T, int m, int n> 
Matrix<T, m, n>::~Matrix(){} 

template<class T, int m, int n> 
void Matrix<T, m, n>::print() 
{ 
    for (int i = 0; i < nrow; i++){ 
     for (int j = 0; j < ncol; j++){ 
      cout << elements[i][j] << " "; 
     } 
    cout << endl; 
    } 
} 

template<class T, int m, int n, int l> 
Matrix<T, m, l> operator*(const Matrix<T, m, n>& m1, const Matrix<T, n, l>& m2){ 
    int nrow = m1.nrow; 
    int ncol = m2.ncol; 
    Matrix<T, m, l> m3; 
    for (int i = 0; i < nrow; ++i){ 
     for (int j = 0; j < ncol; ++j){ 
      m3.elements[i][j] = 0; 
      for (int k = 0; k < m1.ncol; k++){ 
       T temp = m1.elements[i][k] * m2.elements[k][j]; 
       m3.elements[i][j] = temp + m3.elements[i][j]; 
      } 
     } 
    } 
return m3; 
} 

//main.cpp 
#include "Matrix.h" 
using namespace std; 

int main() 
{ 
Matrix<int, 3, 2> a; 
Matrix<int, 2, 1> b; 
Matrix<int, 3, 1> c; 
c = a*b; 

c.print(); 
} 

この問題は、おそらくテンプレートのコーディングエラーのために行列の乗算に発生します。これに

./matrix.cpp:48:28: error: function template partial specialization is not allowed 
    friend Matrix<T, m, l> operator*<>(const Matrix<T, m, n>&, const Matrix<T, n, l>&); 
         ^  ~~ 
1 error generated. 

変更、それを::

friend Matrix<T, m, l> operator*(const Matrix<T, m, n>&, const Matrix<T, n, l>&); 
+0

ベクトルのベクトルが悪い:配列がそのように「ギザギザ」であってはいけません。 – Yakk

答えて

1

私はリチャードが変更内容を変更しなければならなかったが、私はまた、operator*の宣言を変更しなければならなかったとfriendなど

+0

それは正解です!行番号と等しい各エントリで行列を初期化しました。例えば、3x2行列の場合、私は[0,0; ​​1,1; 2,2]と2x1行列[0; 1]を持ちますので、それらを乗算して[0; 1; 2]を得ます。本当にありがとう^^私はすぐに質問があります。なぜ私は別のクラス_Tと整数_mと_nが必要ですか?私は行列を宣言するときに、すでにそれら(クラスT、int m、int)をインスタンス化します。 ^^ – SungwonAhn

+0

簡単な答えは、クラス 'T'、' m'、 'n'を使用しようとするとコンパイラが苦情を言いました。私は簡単な方法を取って、それに別の名前を付けて、それはちょうど働いた。クラスの外で 'operator *'を宣言し、クラス内の 'operator *'を行うのではなく 'friend'を使うことと多分関係します。私はクラス内の '演算子* 'を正しく動作させることができましたが、少し奇妙に見えます。 – user2475059

1

エラーはこちら次のとおりです。

template<class T, int m, int n, int l> 
Matrix<T, m, l> operator*(const Matrix<T, m, n>&, const Matrix<T, n, l>&); 
      //^here 

および:

template<class _T, int _m, int _n, int l> 
friend Matrix<_T, _m, l> operator*(const Matrix<_T, _m, _n>&, const Matrix<_T, _n, l>&); 
私は前方宣言はさらに下のインスタンスと一致していないので、私は、これらの2の最初を変更しなかった場合、「 operator*があいまいです」エラーが発生しました。

それが現在の出力です:かなり右思えないが、私はさらにデバッグするために十分目を覚ましていないよん

0 
1 
2