C++模板,分配“静态”和“静态”的对象的问题和“动态” 2
我今天的第二个问题与第一个问题类似。这段代码有什么问题?
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误 1 错误 C2825: 'Item': 后跟 '::
Here Item = 'Sample *' => 时必须是类或命名空间这是一个指针,无论它的目标是什么,指针仍然是一个包含内存地址的普通旧整数,并且没有像 Type 这样的属性。
像这样的东西应该可以解决问题
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
您的专业化创建了
Item
的向量
,但其operator[]
尝试返回一个Item*
。要么更改
operator[]
以返回Item&
:或者实际上返回一个
Item*
,就像签名所说的那样:Your specialization creates a
vector
ofItem
, but itsoperator[]
tries to return anItem*
.Either change
operator[]
to return anItem&
:Or actually return an
Item*
like the signature says it will: