Jesse Brands
Jesse Brands

Reputation: 2877

Class Templates, Matrices and Addition

I'm wading into the world of Operator overloading. I already had some success doing operator overloading on my Vector2 class, I'm now working on a Matrix class and am trying to write a function that will add two matrices together into one new matrix. I'm stumbling on this error though, and Googling it doesn't get me anywhere further as people seem to have an entirely different problem but the same issue.

Here's class declaration:

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

And here is the offending function that isn't working (these are lines 31 to 45 of matrix.cpp):

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;
}

And last but not least, here are the two errors I'm getting.

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.

Anyone have an idea what's going on here? It's probably really simple and I'm being daft. I need some coffee :|

Sincerely,

Jesse

Upvotes: 0

Views: 1809

Answers (1)

hansmaad
hansmaad

Reputation: 18905

'Mage::Matrix' : use of class template requires template argument list

In the definition of operator+:

 template<class T>
 Matrix<T> Matrix<T>::operator+(const Matrix<T>& A)
        ^

Upvotes: 1

Related Questions