D
daniele lugli
Dear all,
this class:
template<int m, int n>
class SmallMatrix
{
typedef double myData [m] [n];
myData M;
public:
SmallMatrix () {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
M[j] = 0.;
}
}
}
virtual ~SmallMatrix () {}
operator myData & () {
return M;
}
operator const myData & () const {
return M;
}
};
compiles fine with g++-4.4.3 and does what I expect it to do, eg I can
declare a SmallMatrix<3,4> m; and then access its elements as m[j].
Then I tried to add an operator* like this:
SmallMatrix<m, n1> operator* (const SmallMatrix<n, n1> & f) const {
SmallMatrix<m, n1> result;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n1; j++) {
result[j] = 0.;
for (int k = 0; k < n; k++) {
result[j] += M[k] * f[k][j];
}
}
}
return result;
}
what I would like to do is:
SmallMatrix<2, 3> m23;
SmallMatrix<3, 4> m34;
SmallMatrix<2, 4> m24;
....
m24 = m23 * m34;
but the class SmallMatrix with the operator* method in it does not
compile; I get:
'n1' was not declared in this scope
template argument 2 is invalid
clearly because n1 is not known before it appears in the return type
declaration; it becomes known only after the argument of operator* is
parsed.
I cannot see any way of getting what I would like to have. Is it
possible or it is one of the things one cannot do with c++?
TYIA
this class:
template<int m, int n>
class SmallMatrix
{
typedef double myData [m] [n];
myData M;
public:
SmallMatrix () {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
M[j] = 0.;
}
}
}
virtual ~SmallMatrix () {}
operator myData & () {
return M;
}
operator const myData & () const {
return M;
}
};
compiles fine with g++-4.4.3 and does what I expect it to do, eg I can
declare a SmallMatrix<3,4> m; and then access its elements as m[j].
Then I tried to add an operator* like this:
SmallMatrix<m, n1> operator* (const SmallMatrix<n, n1> & f) const {
SmallMatrix<m, n1> result;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n1; j++) {
result[j] = 0.;
for (int k = 0; k < n; k++) {
result[j] += M[k] * f[k][j];
}
}
}
return result;
}
what I would like to do is:
SmallMatrix<2, 3> m23;
SmallMatrix<3, 4> m34;
SmallMatrix<2, 4> m24;
....
m24 = m23 * m34;
but the class SmallMatrix with the operator* method in it does not
compile; I get:
'n1' was not declared in this scope
template argument 2 is invalid
clearly because n1 is not known before it appears in the return type
declaration; it becomes known only after the argument of operator* is
parsed.
I cannot see any way of getting what I would like to have. Is it
possible or it is one of the things one cannot do with c++?
TYIA