Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/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++_Templates - Fatal编程技术网

C++ 编译器错误:模板类分配不匹配

C++ 编译器错误:模板类分配不匹配,c++,templates,C++,Templates,我正试图创建一个解决方案,使用隐含指针来隐藏库的大部分内部,同时保留基本功能。目前我依赖于模板,下面的代码会产生以下编译器错误,我不知道如何解决: prog.cpp: In function ‘int main()’: prog.cpp:55:10: error: no match for ‘operator=’ (operand types are ‘std::auto_ptr<TypeAdapter_impl<mytype> >’ and ‘TypeAdapter_i

我正试图创建一个解决方案,使用隐含指针来隐藏库的大部分内部,同时保留基本功能。目前我依赖于模板,下面的代码会产生以下编译器错误,我不知道如何解决:

prog.cpp: In function ‘int main()’:
prog.cpp:55:10: error: no match for ‘operator=’ (operand types are ‘std::auto_ptr<TypeAdapter_impl<mytype> >’ and ‘TypeAdapter_impl<mytype>*’)  t._impl = tam;
prog.cpp:在函数“int main()”中:
prog.cpp:55:10:错误:与“operator=”不匹配(操作数类型为“std::auto_ptr”和“TypeAdapter_impl*)t.。_impl=tam;
代码如下:

#include <iostream>
#include <memory>
using namespace std;

typedef long document_ptr;

class mytype {
    public:
    mytype(document_ptr d) {
        a = d;
        std::cout << "Instantiated mytype with document_ptr " << a << "\n";
    }
    document_ptr a;
};

class TypeContainer {
public:
    void * _t; 
    TypeContainer(void * t) {  _t = t; }
    ~TypeContainer() { delete _t; }
    // TODO operator *
};

class DocumentContainer {
    public:
    document_ptr * doc;
};

template<class Type>
class TypeAdapter_impl
{
    public:
    TypeAdapter_impl() { }
    ~TypeAdapter_impl() { }

    TypeContainer TypeFactory(DocumentContainer& d){
        Type * t = new Type(d.doc);
        return TypeContainer(t);
    }
};

template<class Type>
class TypeAdapter
{
    public:
    std::auto_ptr< TypeAdapter_impl<Type> > _impl;
};



int main() {
    // your code goes here
    TypeAdapter<mytype> t;
    TypeAdapter_impl<mytype> * tam = new TypeAdapter_impl<mytype>;
    t._impl = tam;
    DocumentContainer d;
    d.doc = new document_ptr(10);
    mytype m = t._impl->TypeFactory(d);
    return 0;
}
#包括
#包括
使用名称空间std;
typedef long document_ptr;
类mytype{
公众:
mytype(文件类型){
a=d;

您在问题中提到的错误是由以下原因引起的:

  • 如果RHS类型为
    T*
    ,则在
    std::auto_ptr
    中没有
    operator=
  • 构造函数
    std::auto_ptr::auto_ptr(T*)
    是显式的()
  • 这是行不通的

    std::auto_ptr<int> a;
    int* b = new int;
    a = b;
    

    由于
    \u t
    的类型为
    void*
    ,因此不起作用

    mytype m = t._impl->TypeFactory(d);
    
    由于
    TypeAdapter\u impl::TypeFactory()
    的返回类型为
    TypeContainer
    ,并且无法将
    TypeContainer
    转换为
    mytype
    ,因此将不起作用

    线路

        Type * t = new Type(d.doc); // FIXME document_ptr not defined here!
    
    也不正确。
    doc
    ,如
    main
    中定义的,指向一个包含10个元素的数组。不确定您在此处使用哪个元素。将其更改为:

        Type * t = new Type(d.doc[0]);
    

    删除编译器错误。

    我已从代码中删除注释,但没有更新它以反映您的更正。您提到的最后一行确实应该取消对指针d.doc的引用,但它不是数组,只是一个长整数。
    ~TypeContainer() { delete _t; }
    
    mytype m = t._impl->TypeFactory(d);
    
        Type * t = new Type(d.doc); // FIXME document_ptr not defined here!
    
        Type * t = new Type(d.doc[0]);