开发者

C++ templates, problem with object allocated both "static" and "dynamic" 2

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  
Contai开发者_StackOverflow中文版ner <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

template <typename T>
struct traits {
    typedef typename T::Type Type;
};

template<typename T>
struct traits<T*> {
    typedef typename traits<T>::Type Type;
};

template <typename Item> 
class Process
{
public:
    void process (Container <Item>*c) 
    {
        typename traits<Item>::Type var;
    }
};


Your specialization creates a vector of Item, but its operator[] tries to return an Item*.

Either change operator[] to return an Item&:

Item& operator [](int index) { return items[index]; }

Or actually return an Item* like the signature says it will:

Item* operator [](int index) { return &items[index]; }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜