“No matching function for call error” with g++

前端 未结 4 1398
悲&欢浪女
悲&欢浪女 2021-01-28 03:47

I have a class A

template
class A : public std::auto_ptr
{
    typedef std::auto_ptr Super;
public:
    A() : Super() { }
            


        
相关标签:
4条回答
  • 2021-01-28 04:26

    Without the exact code that actually causes the error it's hard to know for sure, but I suspect that from AIR::A<T>::A(AIR::A<T>&) what happening is you're trying to pass a temporary (possibly implicit) object into AIR::A<T>::A and MSVC lets you bind the temporary to the non-const reference parameter, while g++ quite properly prohibits this. Can you make the parameter const?

    0 讨论(0)
  • 2021-01-28 04:27

    I'd guess you're lacking A(A<T> const & o).

    VS (incorrectly) permits getting a non-const reference to a temporary, g++ behaves properly

    0 讨论(0)
  • 2021-01-28 04:39

    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_ptrs. 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).

    0 讨论(0)
  • 2021-01-28 04:41

    It looks compiler talks that it cannot choose right function: constructor A from pointer A<T>::A(T*) or copy constructor A<T>::A(A<T> &). Maybe it worth to change definition to explicit A(T* t) : Super(t) { }

    0 讨论(0)
提交回复
热议问题