Мой второй вопрос сегодня похож на первый. Что не так в этом коде?
#include <vector>
template <typename Item>
struct TItemsList
{
typedef std::vector <Item> Type;
};
Контейнер предметов:
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
};
Метод «процесс» должен уметь работать с контейнером объектов, выделенных как «статическим», так и «динамическим» ...
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;
}
};
Первый вариант работает, но второй не
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);
}
Как спроектировать класс / метод «процесс», чтобы можно было работать с контейнером объектов, выделенных как «статическим», так и «динамическим»? Спасибо за вашу помощь ..
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