Why does my use of operator* fail? - c++

I asked a question earlier yesterday and got some very good answers for sure.
Now I am at the end of my project and I am stuck again and can't figure out the answer.
I am going to put the most pertinent part of my code here and hope to get some insight from you all.
The requirements are: I cannot change the code in my main.cpp, and my header file is supposed to be as simple as possible.
Having that out of the way here is the code: This is my Matrix.h file
#ifndef MATRIX_H
#define MATRIX_H
#include <vector>
#include <iostream>
#include <math.h>
#include <complex>
using namespace std;
namespace theMatrix {
template <typename T, size_t ROWS, size_t COLS>
class Matrix {
friend class Matrix;
public:
Matrix(const T & init = T()) : elts(ROWS, vector<T>(COLS, init)) {
};
const vector<T> & operator[](int ROWS) const {
return elts[ROWS];
};
vector<T> & operator[](int ROWS) {
return elts[ROWS];
};
//matrixMult
template<size_t INNER>
Matrix & matrixMult(const Matrix<T, ROWS, INNER> & mat1, const Matrix<T, INNER, COLS> & mat2) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
//elts[i][j] = 0;
for (int k = 0; k < INNER; k++) {
this->elts[i][j] += mat1.elts[i][k] * mat2.elts[k][j];
}
}
}
return *this;
};
//print function
void print(ostream & out) const {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
out << elts[i][j];
}
out << "\n";
}
};
private:
vector< vector<T> > elts;
};
//Operator<<
template <typename T, size_t ROWS, size_t COLS>
ostream & operator<<(ostream & out, const Matrix<T, ROWS, COLS> & elts) {
elts.print(out);
return out;
};
//Operator*
template <typename T, size_t ROWS, size_t COLS>
Matrix<T, ROWS, COLS> operator*(const Matrix<T, ROWS, COLS> & lhs, const Matrix<T, ROWS, COLS> & rhs) {
Matrix<T, ROWS, COLS> returnVal;
return returnVal.matrixMult(lhs, rhs);
};
//operator matrixMult
template <typename T, size_t ROWS, size_t INNER, size_t COLS>
inline void matrixMult(const Matrix<T, ROWS, INNER> & mat1, const Matrix<T, INNER, COLS> & mat2, Matrix<T, ROWS, COLS> & mat3) {
mat3 = matrixMult(mat1, mat2);
};
This is my main.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib> // for rand()
#include "Matrix.h"
using namespace std;
using namespace theMatrix;
template <typename T, size_t ROWS, size_t COLS>
void randomize(Matrix<T, ROWS, COLS> & mat)
// Put random values in a Matrix.
// Note: It must be possible to assign T an int value.
{
for (size_t i = 0; i < ROWS; i++)
for (size_t j = 0; j < COLS; j++)
mat[i][j] = (rand() % 21) - 10; // Random number in range -10,...,+10
}
struct Complex
{
Complex(double re = 0.0, double im = 0.0) : real(re), imag(im) { }
Complex & operator+=(const Complex & rhs)
{
real += rhs.real;
imag += rhs.imag;
return *this;
}
Complex & operator-=(const Complex & rhs)
{
real -= rhs.real;
imag -= rhs.imag;
return *this;
}
Complex & operator*=(const Complex & rhs)
{
real = real * rhs.real - imag * rhs.imag;
imag = real * rhs.imag + imag * rhs.real;
return *this;
}
double real;
double imag;
};
Complex operator+(const Complex & lhs, const Complex & rhs)
{
return Complex(lhs.real + rhs.real, lhs.imag + rhs.imag);
}
Complex operator-(const Complex & lhs, const Complex & rhs)
{
return Complex(lhs.real - rhs.real, lhs.imag - rhs.imag);
}
Complex operator*(const Complex & lhs, const Complex & rhs)
{
return Complex(lhs.real * rhs.real - lhs.imag * rhs.imag, lhs.real * rhs.imag + lhs.imag * rhs.real);
}
ostream & operator<<(ostream & out, const Complex & c)
{
out << "(" << c.real << " + " << c.imag << "i)";
return out;
}
int main()
{
// Matrix multiplication tests:
Matrix<int, 2, 3> m4;
randomize(m4);
Matrix<int, 3, 5> m5;
randomize(m5);
Matrix<int, 2, 5> m6;
Matrix<int, 2, 5> m7;
matrixMult(m4, m5, m7);
out << "m6 == m4 * m5: " << (m6 == m4 * m5) << endl; // here is the first error
// Matrices of Complex:
Matrix<Complex, 2, 8> m11;
randomize(m11);
Complex c(1, -3);
Matrix<Complex, 8, 3> m12;
randomize(m12);
out << "m11 * m12: " << endl << m11 * m12 << endl; // Here is the second error
out.close();
}
I am having only two errors which are conflicting with the complex operator * declaration which I have been trying to solve for several hours and I just can't figure it out.
Here are the errors:
Error 1 error C2678: binary '*' : no operator found which takes a left-hand operand of type 'nkumath::Matrix<T,ROWS,COLS>' (or there is no acceptable conversion)
Error 2 error C2678: binary '*' : no operator found which takes a left-hand operand of type 'nkumath::Matrix<T,ROWS,COLS>' (or there is no acceptable conversion)
Thanks again for everyone's help on this.
EDIT: Here is the solution! which I voted for. Thanks!!!
//Operator*
template <typename T, size_t ROWS, size_t INNER, size_t COLS>
Matrix<T, ROWS, COLS> operator*(const Matrix<T, ROWS, INNER> & lhs, const Matrix<T, INNER, COLS> & rhs) {
Matrix<T, ROWS, COLS> returnVal;
return returnVal.matrixMult(lhs, rhs);
};

Your templated operator * overload does not allow the two input matrices to differ in size.

Your problem is that the templated operator* requires both sides of the multiplication to be the same Matrix instantiation. T, ROWS and COLS can't be deduced to different values for the type of lhs and rhs in the same function.
template <typename T, size_t ROWS, size_t COLS> Matrix<T, ROWS, COLS> operator*(const Matrix<T, ROWS, COLS> & lhs, const Matrix<T, ROWS, COLS> & rhs);
Matrix<int, 2, 3> m4;
Matrix<int, 3, 5> m5;
m4*m5;
If the compiler deduces ROWS and COLS as 2 and 3 for the above multiplication, the rhs type won't match your templated operator*. And if the compiler deduces ROWS as 3 and COLS as 5, the lhs type won't match.
You'll need to define how multiplication of differently sized Matrix instances should work, and make e.g:
template <typename T, size_t ROWS_L, size_t COLS_L, size_t ROWS_R, size_t COLS_R>
Matrix<T, ROWS_L, COLS_L> operator*(const Matrix<T, ROWS_L, COLS_L> & lhs, const Matrix<T, ROWS_R, COLS_R> & rhs);

Related

Operator overload error in class template due to template arguments not being constant expressions

I'm writing a simple matrix class that uses a template to define the datatype, as well as the number of rows and columns. However, I'm running into some issues when defining operator overloads. Here is the class so far:
template<typename T, size_t num_rows, size_t num_cols>
class Matrix {
public:
Matrix();
Matrix(const Matrix &m); // Copy constructor
~Matrix();
T operator()(const size_t row, const size_t col) const;
T& operator()(const size_t row, const size_t col);
Matrix& operator=(const Matrix &m); // Copy assignment operator
Matrix operator*(const Matrix &m2) const;
private:
T data_[num_rows][num_cols];
size_t num_rows_;
size_t num_cols_;
};
template<typename T, size_t num_rows, size_t num_cols>
Matrix<T, num_rows, num_cols>::Matrix() : num_rows_(num_rows), num_cols_(num_cols) {}
template<typename T, size_t num_rows, size_t num_cols>
Matrix<T, num_rows, num_cols>::~Matrix() {}
// Copy constructor
template<typename T, size_t num_rows, size_t num_cols>
Matrix<T, num_rows, num_cols>::Matrix(const Matrix &m) :
num_rows_(m.num_rows_),
num_cols_(m.num_cols_)
{
for(size_t i = 0; i < this->num_rows_; ++i) {
for(size_t j = 0; j < this->num_cols_; ++j) {
(*this)(i,j) = m(i,j);
}
}
}
template<typename T, size_t num_rows, size_t num_cols>
T Matrix<T, num_rows, num_cols>::operator()(const size_t row, const size_t col) const {
return data_[row][col];
}
template<typename T, size_t num_rows, size_t num_cols>
T& Matrix<T, num_rows, num_cols>::operator()(const size_t row, const size_t col) {
return data_[row][col];
}
// Copy assignment operator
template<typename T, size_t num_rows, size_t num_cols>
Matrix<T, num_rows, num_cols>& Matrix<T, num_rows, num_cols>::operator=(const Matrix &m) {
if(this == &m){
return *this;
}
for(size_t i = 0; i < this->num_rows_; ++i) {
for(size_t j = 0; j < this->num_cols_; ++j) {
(*this)(i,j) = m(i,j);
}
}
return *this;
}
// Multiplication operator overload
template<typename T, size_t num_rows, size_t num_cols>
Matrix<T, num_rows, num_cols> Matrix<T, num_rows, num_cols>::operator*(const Matrix &m2) const {
const size_t rows = this->num_rows_;
const size_t cols = m2.num_cols_;
Matrix<float, rows, cols> m3;
for(size_t i = 0; i < this->num_rows_; ++i) {
for(size_t j = 0; j < m2.num_cols_; ++j) {
m3(i,j) = 0.0f;
for(size_t k = 0; k < this->num_cols_; ++k) {
m3(i,j) += (*this)(i,k)*m2(k,j);
}
}
}
return m3;
}
Looking at the multiplication operator overload (please forgive the very slow matrix-matrix product implementation), the problem occurs when I try and define Matrix<float, rows, cols> m3. The error I get says error: non-type template argument is not a constant expression, which occurs because the dimensions of m3 are dependent on the dimensions of this and m2. However, it seems that template arguments have to be known at compile time, and therefore I can't use const size_t rows = this->num_rows_ and const size_t cols = m2.num_cols_ when instantiating m3.
As such, I'm not too sure how I can get the multiplication operator overload to work (and other operator overloads that require returning a new matrix as the result of the operation), since I'm unable to create a matrix to return. Is there a way to keep the current template (i.e. datatype, rows and cols), and still get the multiplication operator overload to work?
Make these two member variables static constexpr and initialize them from the template arguments, since they are just names for the template arguments there's no reason to store them in memory, they are known at compile time:
size_t num_rows_;
size_t num_cols_;
Also note that your copy constructor (and probably assignment operator) are pointless, the compiler would do the job for you if you didn't declare those.
Matrix operator*(const Matrix &m2) const;
this is wrong.
template<std::size_t Y>
Matrix<T, num_rows,Y> operator*(const Matrix<T, num_cols, Y> &m2) const;
this is correct..
Also, delete
size_t num_rows_;
size_t num_cols_;
or make them constexpr or enum constants.
The template multiplication operator will have the 3 dimensions as part of its template arguments.
// Multiplication operator overload
template<typename T, size_t num_rows, size_t num_cols>
template<size_t Y>
Matrix<T, num_rows, Y> Matrix<T, num_rows, num_cols>::operator*(const Matrix<T,num_cols,Y> &m2) const {
you know the cols/rows of each variable from their types.

Template Matrix scalar multiplication c++

When I try to compile the following coding, I have get an error saying
Error 1 error LNK2019: unresolved external symbol "class Matrix __cdecl operator*(int const &,class Matrix &)" (??D#YA?AV?$Matrix#H$02$01##ABHAAV0##Z) referenced in function _main C:\Users\pc\Documents\Visual Studio 2013\Projects\Homework4\Homework4\main.obj Homework4
This error is occurred from the scalar multiplication on matrix and I don't know how to fix it. Can anyone give me an advice on this?
//Matrix.h
#pragma once
#include <iostream>
#include <vector>
using namespace std;
template<class T, int m, int n>
class Matrix
{
vector<vector<T>> elements;
int nrow;
int ncol;
public:
Matrix();
~Matrix();
void print();
vector<T> row(int);
vector<T> col(int);
friend Matrix<T, m, n> operator+<>(const Matrix<T, m, n>&, const Matrix<T, m, n>&);
friend Matrix<T, m, n> operator-<>(const Matrix<T, m, n>&, const Matrix<T, m, n>&);
friend Matrix<T, m, n> operator*(const T&, Matrix<T, m, n>&);
};
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>
vector<T> Matrix<T, m, n>::row(int i)
{
return elements[i];
}
template<class T, int m, int n>
vector<T> Matrix<T, m, n>::col(int j)
{
vector<T> col;
for (int i = 0; i < ncol; i++)
{
col.push_back(elements[i][j]);
}
return col;
}
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>
Matrix<T, m, n> operator+(const Matrix<T, m, n>& m1, const Matrix<T, m, n>& m2){
Matrix<T, m, n> m3;
int nrow = m1.nrow;
int ncol = m1.ncol;
for (int i = 0; i < nrow; ++i){
for (int j = 0; j < ncol; ++j){
m3.elements[i][j] = m1.elements[i][j] + m2.elements[i][j];
}
}
return m3;
}
template<class T, int m, int n>
Matrix<T, m, n> operator-(const Matrix<T, m, n>& m1, const Matrix<T, m, n>& m2){
Matrix<T, m, n> m3;
int nrow = m1.nrow;
int ncol = m1.ncol;
for (int i = 0; i < nrow; ++i){
for (int j = 0; j < ncol; ++j){
m3.elements[i][j] = m1.elements[i][j] - m2.elements[i][j];
}
}
return m3;
}
template<class T, int m, int n>
Matrix<T, m, n> operator*(const T& c, const Matrix<T, m, n>& m1){
int nrow = m1.nrow;
int ncol = m1.ncol;
Matrix<T, m, n> m2;
for (int i = 0; i < nrow; ++i){
for (int j = 0; j < ncol; ++j){
m2.elements[i][j] = c*m1.elements[i][j];
}
}
return m2;
}
//main.cpp
#include "Matrix.h"
using namespace std;
int main()
{
Matrix<int, 3, 2> a;
Matrix<int, 3, 2> b;
Matrix<int, 3, 2> c;
c = 2*b;
c.print();
}
I think the error comes from the friend function (operation *) in the body of the class, but I don't know how to fix it...
The error occurs because
a) operator* is declared as non-template (you should use <>)
b) if you use <> syntax, you must provide the declaration of the template.
Fix
(Add before Matrix class):
template<class T, int m, int n>
class Matrix;
template<class T, int m, int n>
Matrix<T, m, n> operator*(T&, const Matrix<T, m, n>&);
//class matrix definition here
Add <> to operator* friend declaration:
friend Matrix<T, m, n> operator*<>(T&, const Matrix<T, m, n>&);

C++ operator* overloading

I have the following Matrix class that seems to be working well so far
template<typename T, std::size_t M, std::size_t N>
class Matrix
{
public:
Matrix(const std::initializer_list<std::initializer_list<T>> m)
{
// snip
}
T& operator()(const std::size_t i, const std::size_t j)
{
return m_data.at(i + j * N);
}
const T& operator()(const std::size_t i, const std::size_t j) const
{
return m_data.at(i + j * N);
}
Matrix<T,M,N> operator*(const T n)
{
// snip
}
private:
std::array<T, M * N> m_data;
};
However the overloaded operator* only allows scalar multiplication if the scalar is on the right hand side of the operator. I would like to allow this operation even if the scalar is on the left hand side of the operator, so I tried adding this to the Matrix.hpp file:
template<typename T, std::size_t M, std::size_t N>
Matrix<T,M,N> operator*(const T lhs, const Matrix<T,M,N> &rhs)
{
return rhs * lhs;
}
But this gives me the following error:
In file included from test.cpp:1:0:
Matrix.hpp: In instantiation of ‘Matrix<T, M, N> operator*(T, const Matrix<T, M, N>&) [with T = double; long unsigned int M = 3ul; long unsigned int N = 3ul]’:
test.cpp:21:13: required from here
Matrix.hpp:137:13: error: passing ‘const Matrix<double, 3ul, 3ul>’ as ‘this’ argument of ‘Matrix<T, M, N> Matrix<T, M, N>::operator*(T) [with T = double; long unsigned int M = 3ul; long unsigned int N = 3ul]’ discards qualifiers [-fpermissive]
return rhs * lhs;
If I remove const from the Matrix rhs parameter the code compiles and works correctly. I would like to understand why the code does not compile with the const in place?
You need to make the operator* free functions. (You'll need two of
them, one with the scalar on the right, and one with it on the left.)
You probably also want an operator*=; it may be convenient to use this
operator to implement the two operator*.
When you write operator as the member method then the first argument is always "this" instance of the class. If you want to write scalar * matrix then you must write the operator as non-member function. It is usually done as friend method.
template<typename T>
Matrix<T> operator*(T const& n, Matrix<T> m)
{
// snip
}
template<typename T>
Matrix<T> operator*(Matrix<T> m, T const& n)
{
return n * m;
}

Cannot convert from *const to & in += operator [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I was staring at this all night before I decided to give up and go to sleep - a few hours into it again today, I still don't have it. I am unable to figure out how to change the const-ness and parameters to correctly return (on the operator+=). Any help?
The error pops up at the return this statement in the operator+= overload.
#include <iostream>
#include <vector>
template <typename T> class Matrix {
private:
unsigned rows, cols;
public:
Matrix();
~Matrix();
Matrix(const Matrix<T>& rhs);
Matrix(unsigned _rows, unsigned _cols);
std::vector<std::vector<T>> matrix;
Matrix<T>& operator=(Matrix<T> rhs);
//Matrix mathematical operations
Matrix<T> operator+(const Matrix<T>& rhs);
Matrix<T>& operator+=(const Matrix<T>& rhs);
// Access the individual elements
T& operator()(const unsigned& row, const unsigned& col);
const T& operator()(const unsigned& row, const unsigned& col) const;
// Access the row and column sizes
unsigned get_rows() const;
unsigned get_cols() const;
void swap(Matrix<T>& rhs);
};
template<typename T>
Matrix<T>::Matrix() {}
template<typename T>
Matrix<T>::~Matrix() {}
// Parameter Constructor
template<typename T>
Matrix<T>::Matrix(unsigned _rows, unsigned _cols) {
matrix.resize(_rows);
for (unsigned i = 0; i < _rows; i++)
matrix[i].resize(_cols, 1); // change back to 0 after debug
rows = _rows;
cols = _cols;
}
template<typename T>
Matrix<T>& Matrix<T>::operator=(Matrix<T> rhs) {
swap(rhs);
return *this;
}
template<typename T>
Matrix<T> Matrix<T>::operator+(const Matrix<T>& rhs) {
Matrix<T> result(*this);
result += rhs;
return result;
}
template<typename T>
Matrix<T>& Matrix<T>::operator+=(const Matrix<T>& rhs) {
for (unsigned i = 0; i < rows; i++) {
for (unsigned j = 0; j < cols; j++) {
this->matrix[i][j] += rhs(i, j);
}
}
return this; // error pops up here
}
// Access the individual elements
template<typename T>
T& Matrix<T>::operator()(const unsigned& row, const unsigned& col) {
return this->matrix[row][col];
}
// Access the individual elements (const)
template<typename T>
const T& Matrix<T>::operator()(const unsigned& row, const unsigned& col) const{
return this->matrix[row][col];
}
// Get the number of rows of the matrix
template<typename T>
unsigned Matrix<T>::get_rows() const {
return this->rows;
}
//Get the number of columns of the matrix
template<typename T>
unsigned Matrix<T>::get_cols() const {
return this->cols;
}
template<typename T>
void Matrix<T>::swap(Matrix<T>& rhs) {
using std::swap;
swap(this->rows, rhs.rows);
swap(this->cols, rhs.cols);
swap(this->matrix, rhs.matrix);
}
// for debugging
template<typename T>
void print_matrix(Matrix<T>& matrix) {
for (int i = 0; i < matrix.get_rows(); i++) {
for (int j = 0; j < matrix.get_cols(); j++) {
std::cout << matrix(i, j) << " ";
}
std::cout << " " << std::endl;
}
}
int main(int argc, char **argv) {
Matrix<double> matrix1(5, 5);
Matrix<double> matrix2(5, 5);
// Start testing
Matrix<double> matrix3 = matrix1 + matrix2;
print_matrix(matrix1);
std::getchar();
return 0;
}
In the += operator, you probably want:
Matrix<T>& Matrix<T>::operator+=(const Matrix<T>& rhs) {
//....
return *this;
//^^
}
The linker error is because you did not define:
Matrix(const Matrix<T>& rhs);

Overloading templated class binary operator*

I'm writing a 2D matrix template to learn templates and some C++11 features.
Wrote the following header:
template <class T, unsigned int Rows, unsigned int Columns>
class Matrix2D
{
private:
array<array<T,Columns>, Rows> m_Matrix;
public:
Matrix2D() {}
array<T,Columns>& operator[](unsigned int row) { return m_Matrix[row]; } ;
const array<T,Columns>& operator[](unsigned int row) const { return m_Matrix[row]; } ;
friend Matrix2D operator+ <> (const Matrix2D &lhs, const Matrix2D &rhs);
friend Matrix2D operator* <> (const Matrix2D &lhs, const Matrix2D &rhs);
};
The operator+ works fine - I have an implementation, it compiles, links, and stepped through with the debugger.
Problem is with operator*, for which I get the compilation error
1>...\matrix2d.h(18): error C2143: syntax error : missing ';' before '<'
1>...\matrix2d.h(19) : see reference to class template instantiation 'Matrix2D<T,Rows,Columns>' being compiled
There's no line of code trying to use the operator, so it's the definition itself which is wrong, I just don't understand why.
Can anyone help?
EDIT: (added from comment)
template <class T, unsigned int Rows, unsigned int Columns>
Matrix2D<T, Rows, Columns> operator+ (const Matrix2D<T, Rows, Columns> &lhs, const Matrix2D<T, Rows, Columns> &rhs)
{
Matrix2D<T, Rows, Columns> addResult;
for (unsigned int i = 0; i < Rows; i++)
for (unsigned int j = 0; j < Columns; j++)
addResult[i][j] = lhs[i][j] + rhs[i][j];
return addResult;
}
template <class T, unsigned int Rows, unsigned int Columns>
Matrix2D<T, Rows, Columns> operator* (const Matrix2D<T, lRows, lColumns> &lhs, const Matrix2D<T, rRows, rColumns> &rhs)
{
Matrix2D<T, lRows, rColumns> mulResult;
for(unsigned int i = 0; i < lRows; i++)
for(unsigned int j = 0; j < rColumns; j++)
for (unsigned int k = 0; k < lColumns; k++)
mulResult[i][k] += lhs[i][k] * rhs[k][j];
return addResult;
}
You cannot friend a specialization of an undeclared template function. Of course, declaring the operators before defining the class template will require you to forward declare it as well:
template <class T, unsigned int Rows, unsigned int Columns>
class Matrix2D;
template <class T, unsigned int Rows, unsigned int Columns>
Matrix2D<T, Rows, Columns>
operator+ (const Matrix2D<T, Rows, Columns> &lhs, const Matrix2D<T, Rows, Columns> &rhs);
template <class T, unsigned int Rows, unsigned int Columns>
Matrix2D<T, Rows, Columns>
operator* (const Matrix2D<T, Rows, Columns> &lhs, const Matrix2D<T, Rows, Columns> &rhs);
template <class T, unsigned int Rows, unsigned int Columns>
class Matrix2D
{
private:
array<array<T,Columns>, Rows> m_Matrix;
public:
Matrix2D() {}
array<T,Columns>& operator[](unsigned int row) { return m_Matrix[row]; }
const array<T,Columns>& operator[](unsigned int row) const { return m_Matrix[row]; }
friend Matrix2D operator+ <> (const Matrix2D &lhs, const Matrix2D &rhs);
friend Matrix2D operator* <> (const Matrix2D &lhs, const Matrix2D &rhs);
};
alternatively, you could take the easy way and define separate operator functions for each specialization of Matrix2D:
template <class T, unsigned int Rows, unsigned int Columns>
class Matrix2D
{
private:
array<array<T,Columns>, Rows> m_Matrix;
public:
Matrix2D() {}
array<T,Columns>& operator[](unsigned int row) { return m_Matrix[row]; }
const array<T,Columns>& operator[](unsigned int row) const { return m_Matrix[row]; }
friend Matrix2D operator+ (const Matrix2D &lhs, const Matrix2D &rhs) {
// do stuff that adds.
}
friend Matrix2D operator* (const Matrix2D &lhs, const Matrix2D &rhs) {
// do stuff that multiplies.
}
};
which I would probably use for the simpler overall syntax.
EDIT: Proper multiplication of non-square matrices means that the operator* function would in fact need to be friends of three different specializations of Matrix2D: the type of the left operand, right operand, and result. I think the first approach herein would become untenable. You should either friend all specializations of operator*:
template <class T, unsigned int Rows, unsigned int Columns>
class Matrix2D
{
// ...
template <typename U, typename V, unsigned Rows, unsigned Common, unsigned Columns>
friend Matrix2D<decltype(std::declval<U>()+std::declval<V>()), Rows, Columns>
operator * (const Matrix2D<U, Rows, Common>&,
const Matrix2D<V, Common, Columns>&);
};
or simply make the data public (probably the best approach for a "collection-of-data" class anyhow).
In:
template <class T, unsigned int Rows, unsigned int Columns>
class Matrix2D
{
// ...
friend Matrix2D operator* <> (const Matrix2D &lhs, const Matrix2D &rhs);
};
Matrix2D refer in fact to Matrix2D<T, Rows, Columns> .
And your operator * should be
template <T, unsigned Rows1, unsigned int Common, unsigned int Column>
Matrix2D<T, Row1, Column> operator* (const Matrix2D<T, Row1, Common>& lhs, const Matrix2D<T, Common, Column>& rhs);