Getting a type from a generated class

Hello everyone!

I got a question/problem concerning templates.
Consider following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
template<typename T,int sX,int sY> 
class Grid{

 T data[sX][sY];

 Public:
 //accessors
 int getsX { return sX }
 int getsY { return sY }
 T& operator()(int ix,int iy){ return data[ix][iy]; }
};
 


template<typename GT>
class Solver{

 GT rhs();

 Public:
 
 void sum(){
 
 //typedef ... type
 type sum=0;

 for(int i=0; i<rhs.getsX(); i++)
 for(int j=0; j<rhs.getsY(); j++)
  sum+=rhs(i,j);
 }
};



void main(){
 Solver<Grid<double,20,20>> solve();
};


I know this code doesn't do anything, I just extracted the essential structure to explain my problem, without making things overly complicated. Anyhow, the structure of the code is good as it is and shouldn't be changed. (Also in my real code Grid and Solver are in different .h-files)

Ok: My problem is line 24. What I want to do is define the datatype type so it is the same datatype as T (of class Grid).
I tried something like

typedef typename GT::T data

with little success. (error: No type named 'T' in class 'Grid<double,20,20')
I understand why this can't work but I'm clueless what else to try!

Is it possible to get the datatype of T without making huge detours?
Thanks in advance!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
template<typename T,int sX,int sY> 
 class Grid
 {

     typedef T GridType; //<<========here

     T data[sX][sY];

 public:
     //accessors
     int getsX() { return sX }
     int getsY() { return sY }
     T& operator()(int ix,int iy){ return data[ix][iy]; }
 };



 template<typename GT>
 class Solver{


     GT rhs();

 public:

     void sum()
     {

        typename GT::GridType  sum; //<<=========== and here

         for(int i=0; i<rhs.getsX(); i++)
             for(int j=0; j<rhs.getsY(); j++)
                 sum+=rhs(i,j);
     }
 };
Ahhh ... everything seems so obvious now. Thanks alot!
Topic archived. No new replies allowed.