2012-09-16 3 views
0

나는 연산자 오버로딩 세계를 넘어 가고 있습니다. 저는 이미 Vector2 클래스에서 연산자 오버로딩을 성공적으로 수행했습니다. 이제 Matrix 클래스에서 작업 중이며 두 개의 행렬을 하나의 새로운 행렬에 추가하는 함수를 작성하려고합니다. 나는이 오류에 걸려 넘어지고있다. 그리고 인터넷 검색은 사람들이 완전히 다른 문제가 있지만 같은 문제가있는 것처럼 보이지 않기 때문에 어디에서나 나를 얻을 수는 없다. 여기 클래스 템플릿, 행렬 및 추가

는 클래스 선언의 :

Matrix.h

#ifndef __MAGE2D_MATRIX_H 
#define __MAGE2D_MATRIX_H 

namespace Mage { 
    template<class T> 
    class Matrix { 
    public: 
     Matrix(); 
     Matrix(unsigned int, unsigned int); 
     ~Matrix(); 

     unsigned int getColumns(); 
     unsigned int getRows(); 

     T&  operator[](unsigned int); 
     const T& operator[](unsigned int) const; 
     Matrix operator+(const Matrix&); 
     Matrix operator-(const Matrix&); 

    private: 
     unsigned int rows; 
     unsigned int columns; 
     T*   matrix; 
    }; 
} 

#endif // __MAGE2D_MATRIX_H 

그리고 여기에 작동하지 않는 문제가되는 기능 (이러한 matrix.cpp의 45 라인 31입니다)입니다 :

matrix.cpp

template<class T> 
Matrix Matrix<T>::operator+(const Matrix<T>& A) { 
    if ((rows == A.getRows()) && (columns == A.getColumns())) { 
     Matrix<T> B = Matrix<T>(rows, columns); 
     for (unsigned int i = 0; i <= rows; ++i) { 
      for (unsigned int j = 0; i <= columns; ++i) { 
       B[i][j] = matrix[i][j] + A[i][j]; 
      } 
     } 

     return B; 
    } 

    return NULL; 
} 

마지막으로, 나는 여기에있는 두 가지 오류가 있습니다.

1>ClCompile: 
1> matrix.cpp 
1>src\matrix.cpp(32): error C2955: 'Mage::Matrix' : use of class template requires template argument list 
1>   C:\Users\Jesse\documents\visual studio 2010\Projects\Mage2D\Mage2D\include\Mage2D/Math/Matrix.h(6) : see declaration of 'Mage::Matrix' 
1>src\matrix.cpp(47): error C2244: 'Mage::Matrix<T>::operator +' : unable to match function definition to an existing declaration 
1>   C:\Users\Jesse\documents\visual studio 2010\Projects\Mage2D\Mage2D\include\Mage2D/Math/Matrix.h(17) : see declaration of 'Mage::Matrix<T>::operator +' 
1>   definition 
1>   'Mage::Matrix Mage::Matrix<T>::operator +(const Mage::Matrix<T> &)' 
1>   existing declarations 
1>   'Mage::Matrix<T> Mage::Matrix<T>::operator +(const Mage::Matrix<T> &)' 
1> 
1>Build FAILED. 

누구나 여기서 무슨 일이 벌어지고 있는지 알 수 있습니까? 그것은 아마 아주 간단하고 나는 바보가되고 있습니다. 커피가 필요해. | 감사합니다

,

제시

+0

우선 당신은 increm입니다. 두 번째 루프에서 j 대신 enting i. 먼저 논리적 오류 – xeon111

+0

을 수정하십시오. 그러나 테스트 할 때 쉽게 알 수있는 오류입니다. 그 질문도 아닙니다. 고마워,하지만 이미 그걸 발견 했어. –

답변

1

'마법사 :: 매트릭스'

template<class T> 
Matrix<T> Matrix<T>::operator+(const Matrix<T>& A) 
     ^
: 클래스 템플릿의 사용은 연산자 +의 정의에서 템플릿 인수 목록

이 필요

+0

D' oh! 나는 그것이 어리석은 것을 알았다. 고맙습니다. –