开发者

Smart-pointer template operator overload failing due to error C2244: "unable to match function definition to an existing declaration"

I have looked for dupes of this but forgive me if I missed a solution.

I have a smart pointer that has 2 comparison overloads, "==" and "<". Both of these operate on the underlying pointer only (inherited code before I am chastised). However on compilation I am getting a C2244 error for both. I will present the case for the equality operator only as the other is almost exactly the same:

Error 4 error C2244: 'SmartRef::operator ==' : unable to match function definition to an existing declaration q:\projs\ha\src\SmartRef.cpp 114

The following code works fine in VC4.2 (I am porting to VC2008... courage).

SmartRef.h

#ifndef HA_INCL_REFERENCE
#define HA_INCL_REFERENCE 1

template <class TypeRefObj>
class SmartRef
{
public:

    //@cmember Default constructor
    inline SmartRef();

    //@cmember Constructor
    inline SmartRef( TypeRefObj * pThePointer );

    //@cmember Destructor
    inline ~SmartRef();

    //@cmember Copy constructor
    inline SmartRef( const NIEB_clReference< TypeRefObj > & roTheValue );

    //@cmember
    inline const SmartRef< TypeRefObj > & operator= ( const SmartRef< TypeRefObj > & roTheValue );

    //@cmember
    inline SmartRef< TypeRefObj > & operator= ( const SmartRef * pTheValue );

    //@cmember
    inline int operator== ( const SmartRef< TypeRefObj > & roTheValue ) const;

    //@cmember
    inline int operator< ( const SmartRef< TypeRefObj > & roTheValue ) const;

    //@cmember
    inline TypeRefObj & operator* () const;

   开发者_如何学C //@cmember
    inline TypeRefObj * operator-> () const;

private:
    //@cmember Reference pointer
    TypeRefObj * m_pRefPointer;
};
#include "SmartRef.cpp"
#endif //HA_INCL_REFERENCE

SmartRef.cpp

template <class TypeRefObj> 
inline int SmartRef< TypeRefObj >::operator== 
    ( SmartRef< TypeRefObj > & roTheValue ) const
{
    return m_pRefPointer == roTheValue.m_pRefPointer;
}

My first thought was that there was a problem because there was no comparison in the specialised classes, but then I realised that I am comparing pointers not objects. Then I thought maybe I need to be very explicit about that so I case both sides of the comparison to `void* but that made no difference. So now I am thinking that there is some reason that the definition is not valid in VC 2008. But I cannot for the life of me figure out what is wrong with the definition.

Any help is much appreciated. Thanks, Dennis.

*EDIT:*I'm embarrassed that it was so simple. I was missing a const from the definition. Thanks all, I don't know how I failed to spot it! Prize goes to first correct answer.


You have omitted the const from the function in SmartPtr.cpp

template <class TypeRefObj> 
inline int SmartRef< TypeRefObj >::operator== ( const SmartRef< TypeRefObj > & roTheValue ) const
{
    return m_pRefPointer == roTheValue.m_pRefPointer;
}

Works


You're lacking a const in the definition.

template <class TypeRefObj> 
inline int SmartRef< TypeRefObj >::operator== 
    ( const SmartRef< TypeRefObj > & roTheValue ) const

Also, you should define the member functions in the header - #include of a .cpp like you do doesn't do much for readability.


Templates don't belong in a .cpp file, they belong in a header too, because the compiler needs to know the full code to instantiate templates of it. Then, you are missing the const in the definition of your operator==. Also, all comparision operators should return bool, not int.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜