C++模板,分配“静态”和“静态”的对象的问题和“动态” 2

发布于 2024-10-29 17:35:23 字数 2017 浏览 1 评论 0原文

我今天的第二个问题与第一个问题类似。这段代码有什么问题?

#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 

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦开始←不甜 2024-11-05 17:35:23

错误 1 ​​错误 C2825: 'Item': 后跟 '::

Here Item = 'Sample *' => 时必须是类或命名空间这是一个指针,无论它的目标是什么,指针仍然是一个包含内存地址的普通旧整数,并且没有像 Type 这样的属性。

像这样的东西应该可以解决问题

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;
    }
};

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;
    }
};
似最初 2024-11-05 17:35:23

您的专业化创建了 Item向量,但其operator[] 尝试返回一个Item*

要么更改 operator[] 以返回 Item&

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

或者实际上返回一个 Item*,就像签名所说的那样:

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

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]; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文