My second question today is similar to the first one. What is wrong in this code?
#include <vector>
template <typename Item>
struct TItemsList
{
typedef std::vector <Item> Type;
};
Container of objects:
template <typename Item>
class Container
{
protected:
typename TItemsList <Item>::Type items;
public:
Item & operator [] ( int index ) {return items[index];}
...
//Other functions
};
//Specialization
template <typename Item>
class Container <Item *>
{
protected:
typename TItemsList <Item>::Type items;
public:
Item * operator [] ( int index ) {return items[index];}
...
//Other functions needs to be specialized
};
The method “process” should be able to work with a container of objects allocated both “static” and “dynamic”…
template <typename T>
class Sample
{
public:
T first;
T second;
typedef T Type;
};
template <typename Item>
class Process
{
public:
void process (Container <Item> *c)
{
//Compile errors related to left part of the equation, see bellow, please
typename Item::Type var = (*c)[0].first + (*c)[0].second;
}
};
The first option works but the second not
int main(int argc, _TCHAR* argv[])
{
Container <Sample <double> > c1;
Process <Sample <double> > a1;
a1.process(&c1);
//Dynamic allocation does not work
Container <Sample <double> *> c2;
Process <Sample <double> *> a2;
a2.process(&c2);
}
How to design a class / method “process” so as to be able to work with a container of objects allocated both “static” and “dynamic” ? Thanks for your help..
Error 1 error C2825: 'Item': must be a class or namespace when followed by '::
Error 6 error C2228: left of '.second' must have class/struct/union
Error 5 error C2228: left of '.first' must have class/struct/union
Error 3 error C2146: syntax error : missing ';' before identifier 'var'
Error 4 error C2065: 'var' : undeclared identifier
Error 2 error C2039: 'Type' : is not a member of '`global
Error 1 error C2825: ‘Item’: must be a class or namespace when followed by ‘::
Here Item = ‘Sample *’ => This is a pointer, whatever what it target, pointer remain a plain old integer that contain an memory address, and has no attribute like Type.
Something like that should do the trick