Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.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/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++ ';对';g+错误+;:构造函数或运算符=不匹配?_C++_Templates_G++_Auto Ptr - Fatal编程技术网

C++ ';对';g+错误+;:构造函数或运算符=不匹配?

C++ ';对';g+错误+;:构造函数或运算符=不匹配?,c++,templates,g++,auto-ptr,C++,Templates,G++,Auto Ptr,我有一个作业接线员 AP<T>& operator=(AP<T>& o) {Super::operator=(o); return *this; } AP&operator=(AP&o){Super::operator=(o);返回*this;} 当我期望g++编译器为这段代码找到这个操作符时 AP<System> to = System::Create(); AP to=System::Create(); 我有编译错误 no matc

我有一个作业接线员

AP<T>& operator=(AP<T>& o) {Super::operator=(o); return *this; }
AP&operator=(AP&o){Super::operator=(o);返回*this;}
当我期望g++编译器为这段代码找到这个操作符时

AP<System> to = System::Create();
AP to=System::Create();
我有编译错误

no matching function for call to ‘H::AP<H::System>::AP(H::AP<H::System>)’
ap.cpp:13: note: candidates are: H::AP<T>::AP(H::AP<U>&) [with U = H::System, T = H::System]
ap.cpp:12: note:                 H::AP<T>::AP(H::AP<T>&) [with T = H::System]
ap.cpp:11: note:                 H::AP<T>::AP(T*) [with T = H::System]
调用“H::AP::AP(H::AP)”时没有匹配的函数
ap.cpp:13:注:候选项是:H::ap::ap(H::ap&)[带U=H::System,T=H::System]
ap.cpp:12:注意:H::ap::ap(H::ap&)[带T=H::系统]
ap.cpp:11:note:H::ap::ap(T*)[带T=H::System]
为什么会这样?MSVC编译这段代码没有问题

资料来源如下

#include <memory>

namespace H {

template<typename T>
class AP : public std::auto_ptr<T>
{
    typedef std::auto_ptr<T> Super;
public:
    template<typename U> AP<T>& operator=(AP<U>& o) { Super::operator=(o.release()); return *this; }
    AP<T>& operator=(AP<T>& o) { Super::operator=(o); return *this; }
};

class System {
public:
    static AP<System> Create(); 
};

AP<System> System::Create()
{
    AP<System> a(new System()); 
    return a;
}

int main()
{
    AP<System> to = System::Create();
}
};
#包括
命名空间H{
模板
AP类:公共标准::自动测试
{
typedef std::auto_ptr Super;
公众:
模板AP&operator=(AP&o){Super::operator=(o.release());返回*this;}
AP&operator=(AP&o){Super::operator=(o);返回*this;}
};
阶级制度{
公众:
静态AP-Create();
};
AP系统::创建()
{
AP a(新系统());
返回a;
}
int main()
{
AP to=System::Create();
}
};
补充 使用
AP(const AP&o):Super(o){}
,我得到了这些错误

ap.cpp: In copy constructor ‘H::AP<T>::AP(const H::AP<T>&) [with T = H::System]’:
ap.cpp:33:   instantiated from here
ap.cpp:12: error: passing ‘const H::AP<H::System>’ as ‘this’ argument of ‘std::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>() [with _Tp1 = H::System, _Tp = H::System]’ discards qualifiers
ap.cpp:在复制构造函数'H::ap::ap(const H::ap&)[with T=H::System]'中:
ap.cpp:33:从此处实例化
ap.cpp:12:错误:将“const H::ap”作为“std::auto_ptr::operator std::auto_ptr_ref()[with _Tp1=H::System,_Tp=H::System]”的“this”参数传递将丢弃限定符
补充2 我不知道这是最好的解决方案,但这段代码似乎有效

int main()
{
  H::AP<H::System> tox(H::System::Create().release());
  return 0;
}
intmain()
{
H::AP-tox(H::System::Create().release());
返回0;
}

它正在寻找一个复制构造函数,而不是一个
操作符=
:请仔细阅读错误消息。您的复制构造函数定义不正确:您需要将其定义为
const
引用:

AP(const AP<T>& o) : Super(o) { }
// ^^^^^
AP(const AP&o):超级(o){
// ^^^^^
AP to=System::Create()查找副本构造函数

使用
const
将解决您的问题。除此之外,临时表不能绑定到非常量引用

例如:

AP<System> &ref = AP<System>(); 
AP&ref=AP();
如果副本c-tor的参数不是对
const
的引用,则不会在gcc上编译。然而,在MSVC++上,上述代码可以编译,因为MSVC++(2008年或之前)允许临时对象绑定到非常量引用(邪恶扩展)


但是,如果您试图在正确的情况下复制
auto_ptr
的副本c-tor

,请注意他正在使用
auto_ptr
。他需要执行与处理临时变量相同的技巧。谢谢你的回答,但我在添加常量时遇到了一些错误。这不是临时变量的精确副本吗?临时变量不绑定到MSVC2010中的非常量引用。您需要澄清哪些版本确实有此扩展。