V
veganders
When making a general matrix class I came across the following
situation that I don't have an explanation for. For some friend
functions I have to declare them in advance while other works anyway.
Could anyone care to explain why I have to do it in this way?
Best regards,
Anders
Code follows below
//operator+ here needs to be declared in advanced or I get a compiler
error while operator* works anyway
template<typename T, int ROWS, int COLS>
class Matrix;
template<typename T, int ROWS, int COLS>
Matrix<T, ROWS, COLS> operator+(
const Matrix<T, ROWS, COLS> & lhs,
const Matrix<T, ROWS, COLS> & rhs);
template<typename T, int ROWS, int COLS>
class Matrix {
public:
Matrix();
// here I have to specify operator+ <T, ROWS, COLS> while
// operator* works fine without
friend Matrix<T, ROWS, COLS> operator+ <T, ROWS, COLS>(
const Matrix<T, ROWS, COLS> & lhs,
const Matrix<T, ROWS, COLS> & rhs);
template<int COLS2>
friend Matrix<T, ROWS, COLS2> operator*(
const Matrix<T, ROWS, COLS> & lhs,
const Matrix<T, COLS, COLS2> & rhs);
template<int ROWS2>
friend Matrix<T, ROWS2, COLS> operator*(
const Matrix<T, ROWS2, ROWS> & lhs,
const Matrix<T, ROWS, COLS> & rhs);
template<int INNER>
friend Matrix<T, ROWS, COLS> operator*(
const Matrix<T, ROWS, INNER> & lhs,
const Matrix<T, INNER, COLS> & rhs);
private:
T mData[COLS][ROWS];
};
template<typename T, int ROWS, int COLS>
Matrix<T, ROWS, COLS>::Matrix() {};
template<typename T, int ROWS, int COLS>
Matrix<T, ROWS, COLS> operator+(
const Matrix<T, ROWS, COLS> & lhs,
const Matrix<T, ROWS, COLS> & rhs) {
Matrix<T, ROWS, COLS> tmp;
for ( int i = 0; i < COLS; i++ ) {
for ( int j = 0; j < ROWS; j++ ) {
tmp.mData[j] = lhs.mData[j] + rhs.mData[j];
}
}
return tmp;
}
// Matrix - Matrix multiplication
template<typename T, int ROWS, int INNER, int COLS>
Matrix<T, ROWS, COLS> operator*(
const Matrix<T, ROWS, INNER> & lhs,
const Matrix<T, INNER, COLS> & rhs) {
Matrix<T, ROWS, COLS> tmp;
for ( int i = 0; i < COLS; i++ ) {
for ( int j = 0; j < ROWS; j++ ) {
tmp.mData[j] = 0;
}
}
for ( int i = 0; i < INNER; i++ ) {
for ( int j = 0; j < COLS; j++ ) {
for ( int k = 0; k < ROWS; k++ ) {
tmp.mData[j][k] += lhs.mData[k] * rhs.mData[j];
}
}
}
return tmp;
}
int main() {
Matrix<float,2,2> A, B;
A+B;
A*B;
return 0;
}
situation that I don't have an explanation for. For some friend
functions I have to declare them in advance while other works anyway.
Could anyone care to explain why I have to do it in this way?
Best regards,
Anders
Code follows below
//operator+ here needs to be declared in advanced or I get a compiler
error while operator* works anyway
template<typename T, int ROWS, int COLS>
class Matrix;
template<typename T, int ROWS, int COLS>
Matrix<T, ROWS, COLS> operator+(
const Matrix<T, ROWS, COLS> & lhs,
const Matrix<T, ROWS, COLS> & rhs);
template<typename T, int ROWS, int COLS>
class Matrix {
public:
Matrix();
// here I have to specify operator+ <T, ROWS, COLS> while
// operator* works fine without
friend Matrix<T, ROWS, COLS> operator+ <T, ROWS, COLS>(
const Matrix<T, ROWS, COLS> & lhs,
const Matrix<T, ROWS, COLS> & rhs);
template<int COLS2>
friend Matrix<T, ROWS, COLS2> operator*(
const Matrix<T, ROWS, COLS> & lhs,
const Matrix<T, COLS, COLS2> & rhs);
template<int ROWS2>
friend Matrix<T, ROWS2, COLS> operator*(
const Matrix<T, ROWS2, ROWS> & lhs,
const Matrix<T, ROWS, COLS> & rhs);
template<int INNER>
friend Matrix<T, ROWS, COLS> operator*(
const Matrix<T, ROWS, INNER> & lhs,
const Matrix<T, INNER, COLS> & rhs);
private:
T mData[COLS][ROWS];
};
template<typename T, int ROWS, int COLS>
Matrix<T, ROWS, COLS>::Matrix() {};
template<typename T, int ROWS, int COLS>
Matrix<T, ROWS, COLS> operator+(
const Matrix<T, ROWS, COLS> & lhs,
const Matrix<T, ROWS, COLS> & rhs) {
Matrix<T, ROWS, COLS> tmp;
for ( int i = 0; i < COLS; i++ ) {
for ( int j = 0; j < ROWS; j++ ) {
tmp.mData[j] = lhs.mData[j] + rhs.mData[j];
}
}
return tmp;
}
// Matrix - Matrix multiplication
template<typename T, int ROWS, int INNER, int COLS>
Matrix<T, ROWS, COLS> operator*(
const Matrix<T, ROWS, INNER> & lhs,
const Matrix<T, INNER, COLS> & rhs) {
Matrix<T, ROWS, COLS> tmp;
for ( int i = 0; i < COLS; i++ ) {
for ( int j = 0; j < ROWS; j++ ) {
tmp.mData[j] = 0;
}
}
for ( int i = 0; i < INNER; i++ ) {
for ( int j = 0; j < COLS; j++ ) {
for ( int k = 0; k < ROWS; k++ ) {
tmp.mData[j][k] += lhs.mData[k] * rhs.mData[j];
}
}
}
return tmp;
}
int main() {
Matrix<float,2,2> A, B;
A+B;
A*B;
return 0;
}