Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;模板,对象分配有问题;“静态”;及;“动态”;_C++_Templates - Fatal编程技术网

C++ C++;模板,对象分配有问题;“静态”;及;“动态”;

C++ C++;模板,对象分配有问题;“静态”;及;“动态”;,c++,templates,C++,Templates,这个代码有什么问题 template <typename T> class Sample { public: T first; T second; typedef T Type; }; and template <typename Item> class Process { public: void process (Item *item) { typename Item::Type var = item-

这个代码有什么问题

template <typename T>
class Sample
{
public:
    T first;
    T second;
    typedef T Type;
};

and 

template <typename Item> 
class Process
{
public:
    void process (Item *item) 
    {
        typename Item::Type var = item->first + item->second;
        //Some code...
    }
};
模板
类样本
{
公众:
T第一;
T秒;
T型;
};
及
模板
类进程
{
公众:
作废流程(项目*项目)
{
typename Item::Type var=Item->first+Item->second;
//一些代码。。。
}
};
方法“进程”应该能够处理分配了“静态”和“动态”的对象……第一个选项有效

int main(int argc, _TCHAR* argv[])
{
Sample <double> s;
Process <Sample <double> > a;
a.process(&s);

return 0;
}
int main(int argc,_TCHAR*argv[]
{
样本s;
过程a;
a、 过程&s;
返回0;
}
但第二个不是

int main(int argc, _TCHAR* argv[])
{

Sample <double> *s = new Sample <double>();
Process <Sample <double> *> a;
a.process(s); //Error  C2664: 'Process<Item>::process' : cannot convert parameter 1 from 'Sample<T> *' to 'Sample<T> *'
return 0;
}
int main(int argc,_TCHAR*argv[]
{
样本*s=新样本();
过程a;
a、 进程;//错误C2664:'process::process':无法将参数1从'Sample*'转换为'Sample*'
返回0;
}
如何设计一个类和方法“进程”,以便能够处理分配了“静态”和“动态”的对象?谢谢你的帮助

Process <Sample <double> *> a;
a.process(s);
致:

过程a;
如果查看完整的错误消息,编译器可能会告诉您在尝试将一个
示例
与另一个
示例
Process::Process()
需要一个
项*
,因此
Process::Process()
需要一个
示例

但是,解决方案要简单得多:

Sample <double> *s = new Sample <double>();
Process <Sample <double> > a;
a.process(s);
Sample*s=newsample();
过程a;
a、 过程;
对于
进程
,对象是如何分配的并不重要,它得到的只是一个指向它的指针并与之配合使用(假设它不会保持所有权或试图删除对象)

现在应该可以了

或者这样做:

Sample <double> *s = new Sample <double>();
Process <Sample <double> *> a; //<------ your version!
a.process( &s );                 //<------ note I'm passing pointer to pointer!
       // ^^ note the ampersand (&)
Sample*s=newsample();

过程a// 问题不在于对象是以某种方式分配的。至少通过任何可移植的标准方法,无法判断对象是在堆栈上分配的,还是在仅给定其指针的空闲存储上分配的。对于“堆栈”对象和“自由存储”对象,通过指针访问对象成员的方式完全相同

实际问题是,在第二个代码段中,您将错误的类型传递给
流程
变量定义:

Process <Sample <double> *> a;
这显然不是你想要的

要解决此问题,请更改

Process <Sample <double> *> a;
过程a;

过程a;
对于后者,
process()
的函数签名将变为
void process(Sample*item)
,这将允许编译两个代码片段

Process <Sample<double> > a; //<<----------- now it's correct!
a.process(s);
Sample <double> *s = new Sample <double>();
Process <Sample <double> *> a; //<------ your version!
a.process( &s );                 //<------ note I'm passing pointer to pointer!
       // ^^ note the ampersand (&)
Process <Sample <double> *> a;
template <typename Item>
class Process
{
public:
    void process (Item *item)
    { /* ... */ }
};
void process (Sample <double>** item)
{ /* ... */ }
Process <Sample <double> *> a;
Process <Sample <double> > a;