How can I set a pointer in a class to an external static data structure?
struct Str {
double **matr; // which type should matr be?
int nx, ny;
template<size_t rows, size_t cols>
void Init(double(&m)[rows][cols], int sx, int sy) {
matr = m; // <-- error
nx = sx; ny = sy;
}
};
...
static double M[3][5] = { { 0.0, 1.0, 2.0, 3.0, 4.0 },
{ 0.1, 1.1, 2.1, 3.1, 4.1 },
{ 0.2, 1.2, 2.2, 3.2, 4.2 } };
Str s;
s.Init(M, 3, 5);
With this code, I get the following compile time error message (Visual C++ 2008/2012):
1> error C2440: '=' : cannot convert from 'double [3][5]' to 'double **'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> see reference to function template instantiation 'void S::Init4<3,5>(double (&)[3][5],int,int)' being compiled
Mis likely to get confusing - you haveMthe static array ofdoublesandMthe template parameter for thevoid Str::Init()function. Which one are you intendingdouble (*m)[M]to refer to? I think you want yourmattrto be simplydouble *mattr, or maybe the uglierdouble (*mattr)[5]...