2012-05-19 2 views
0

당신이 매우 도움이 되었기 때문에 나는 또 다른 질문을 가지고 있습니다.C++ 템플릿을 사용한 Martices 곱셈

템플릿을 사용하여 행렬 곱셈을 구현했지만 코드를 컴파일 할 수 없습니다.

여기 있습니다.

matrix.h :

#ifndef __MATRIX_H__ 
#define __MATRIX_H__ 

template <class T, int rows, int cols> class matrix { 
public: 
    T mat[rows][cols]; 
    matrix(); 
    matrix(T _mat[rows][cols]); 
    matrix operator+(const matrix& b); 
}; 

template <class T, int rows, int cols> matrix <T,rows,cols> :: matrix (T _mat[rows][cols]){ 
    for (int i=0; i<rows; i++){ 
     for (int j=0; j<cols; j++){ 
      mat[i][j] = _mat[i][j]; 
     } 
    } 
} 

template <class T, int rows, int cols> matrix <T,rows,cols> :: matrix(){ 
    for (int i=0; i<rows; i++){ 
     for (int j=0; j<cols; j++){ 
      mat[i][j] = 0; 
     } 
    } 
} 

template <class T, int rows, int cols> matrix <T,rows,cols> matrix <T,rows,cols>::operator+(const matrix<T, rows, cols>& b){ 
    matrix<T, rows, cols> tmp; 
    for (int i=0; i<rows; i++){ 
     for (int j=0; j<cols; j++){ 
      tmp.mat[i][j] = this->mat[i][j] + b.mat[i][j]; 
     } 
    } 
    return tmp; 
} 

template <class T, int rows, int cols> template <int new_cols> matrix <T,rows,new_cols> matrix <T,rows,cols>::operator*(const matrix<T, cols, new_cols>& b){ 
    matrix<T,rows,new_cols> tmp; 
    int i, j, k; 
    T sum; 
    for(i=0; i<rows; i++){ 
     for(j=0; j<new_cols; j++){ 
      sum = 0; 
      for (k=0; k<cols; k++){ 
       sum += this->mat[i][k] * b.mat[k][j]; 
      } 
      tmp.mat[i][j] = sum; 
     } 
    } 
    return tmp; 
} 



#endif 

matrix.cpp :

#include "tar5_matrix.h" 
int main(){ 

    int mat1[2][2] = {1,2, 
         3,4}; 
    int mat2[2][2] = {5,6, 
         7,8}; 
    int res[2][2]; 
    matrix<int, 2, 2> C; 
    matrix<int, 2, 2> D; 
    matrix<int, 2, 2> A = mat1; 
    matrix<int, 2, 2> B = mat2; 
    C = A+B; 
    D = A*B; 

    return 0; 

} 

, 나는 다음과 같은 오류를 얻을 컴파일하려고

1> tar5_matrix.cpp 
1>c:\users\karin\desktop\lior\study\cpp\cpp_project\cpp_project\tar5_matrix.h(68): error C2039: '*' : is not a member of 'matrix<T,rows,cols>' 

1>c:\users\karin\desktop\lior\study\cpp\cpp_project\cpp_project\tar5_matrix.cpp(14): error C2676: binary '*' : 'matrix<T,rows,cols>' does not define this operator or a conversion to a type acceptable to the predefined operator 

알려 주시기 바랍니다.

+0

죄송합니다. 잘못된 회선 번호입니다. 행 52입니다. matrix.h에 정의 된 마지막 함수의 클로저 '}' –

+1

포함 경비원에 이중 밑줄을 사용하지 마십시오. __로 시작하는 이름은 구현을 위해 예약되어 있습니다 (예 : 컴파일러, 표준 라이브러리 및 OS)를 사용할 수 있습니다. __과 함께 이름을 사용하면 시스템 헤더의 이름과 충돌 할 위험이 있습니다. 대신'MATRIX_H' 또는'SOME_PREFIX_MATRIX_H'라고 부르십시오. –

답변

5

클래스 본문에 operator*을 정의하지 않았습니다.

+0

그래,이게 matrix.h에 정의 된 마지막 함수 야. –

+0

@LiorAvramov :하지만 당신은 클래스 바디에 넣지 않았다. –

+0

고마워 ... 고마워. –

0

Oli는 지적했듯이 operator*을 멤버 메서드로 선언하지 않았지만 으로 정의하려고합니다. 여기에있는 두 가지 대안은 선언을 추가하거나 자유 함수로 변경하는 것입니다. 나는 곱셈이 오른편에있는 것보다 왼편에 더 많은 연산이 아니기 때문에 특히 자유 함수 접근법을 선호합니다.

template <typename T, size_t N, size_t M, size_t O> 
matrix<T,M,O> operator*(matrix<T,M,N> const & lhs, 
         matrix<T,N,O> const & rhs) 
{ 
    // ... 
} 
+0

수업에 포함해야 할 신고서를 말씀해 주시겠습니까? –

+0

@PraveenVinny : 더 나은 방법은 내가 대답에서 한 것입니다. 연산자를 멤버로 제공하는 것이 아니라 무료 함수로 제공하는 것입니다 (위 코드 참조). 클래스 내에서 선언 할 필요는 없습니다. 내부에 대한 액세스가 필요하면 친구로 선언하는 것이 좋습니다. –