“没有呼叫错误的匹配函数”与g ++ [英] "No matching function for call error" with g++
问题描述
我有一个类A
template< typename T&
class A:public std :: auto_ptr< T>
{
typedef std :: auto_ptr< T>超;
public:
A():Super(){}
A(T * t):Super(t){}
A :Super(o){}
...
};
和IConstEnumerator
template< typename T>
struct IConstEnumerator
{
...
virtual IEnumerator< T> * GetEnumerator()= 0;
virtual IConstEnumerator< T> * GetEnumerator()const = 0;
};
当我运行此代码
AP < IConstEnumerator< IPort> > pe = node.GetPorts()。GetEnumerator(); ;
我找不到与g ++编译器匹配的错误。
错误:没有匹配函数调用'AIR :: A< AIR :: IConstEnumerator< AIR :: IPort& > :: AP(AIR :: A< AIR :: IConstEnumerator< AIR :: IPort>)'
注意:候选项为:AIR :: A& T>&)[其中T = AIR :: IConstEnumerator< AIR :: IPort>]
note:AIR :: A :: A(T *)[T = AIR :: IConstEnumerator
A类有什么问题?
EDIT
使用复制构造函数似乎解决了这个问题。 A< IConstEnumerator< IPort> >
A(A< T> & o):Super(o){}
^ not const
尝试复制临时对象,非const引用不能绑定到临时对象。 Visual C ++有一个邪恶的扩展,允许这个工作;
如果您试图模仿 auto_ptr
auto_ptr_ref
的东西,用作帮助器允许复制临时 auto_ptr
s。我将介绍如何在接受的答案中实现这一点。 code> std :: auto_ptr 的复制构造函数? 对于值得的,从 std :: auto_ptr
有点奇怪;考虑使用组合而不是继承,如果你可以(在 std :: auto_ptr
中没有很多东西,你可以从中获益)。
I have a class A
template<typename T>
class A : public std::auto_ptr<T>
{
typedef std::auto_ptr<T> Super;
public:
A() : Super() { }
A(T* t) : Super(t) { }
A(A<T>& o) : Super(o) { }
...
};
and IConstEnumerator
template<typename T>
struct IConstEnumerator
{
...
virtual IEnumerator<T>* GetEnumerator() = 0;
virtual IConstEnumerator<T>* GetEnumerator() const = 0;
};
When I run this code
AP< IConstEnumerator<IPort> > pe = node.GetPorts().GetEnumerator(); ;
I got errors from not finding correct match with g++ compiler.
error: no matching function for call to ‘AIR::A<AIR::IConstEnumerator<AIR::IPort> >::AP(AIR::A<AIR::IConstEnumerator<AIR::IPort> >)’
note: candidates are: AIR::A<T>::A(AIR::A<T>&) [with T = AIR::IConstEnumerator<AIR::IPort>]
note: AIR::A<T>::A(T*) [with T = AIR::IConstEnumerator<AIR::IPort>]
What's wrong with the class A? It works well with MSVC.
EDIT
Using copy constructor explicitly seems to solve this issue. A< IConstEnumerator<IPort> > pe(node.GetPorts().GetEnumerator())
Your A
copy constructor takes its argument by non-const reference:
A(A<T>& o) : Super(o) { }
^ not const
Your example probably tries to copy a temporary object and the non-const reference can't bind to the temporary object. Visual C++ has an evil extension that allows this to work; you have to be careful to avoid relying on that extension if you want your code to be portable.
If you are trying to mimic auto_ptr
's copy constructor, you need to also implement something similar to auto_ptr_ref
, which is used as a helper to allow copying temporary auto_ptr
s. I describe how this is accomplished in the accepted answer to How could one implement std::auto_ptr
's copy constructor?
For what it's worth, deriving from std::auto_ptr
is a bit odd; consider using composition instead of inheritance, if you can (there isn't a whole lot in std::auto_ptr
that you'd benefit from by deriving from it anyway).
这篇关于“没有呼叫错误的匹配函数”与g ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!