智能指针模板运算符重载由于错误 C2244 而失败:“无法将函数定义与现有声明匹配”

发布于 2024-10-27 07:22:58 字数 2048 浏览 1 评论 0原文

我一直在寻找这样的骗子,但如果我错过了解决方案,请原谅我。

我有一个智能指针,它有 2 个比较重载:“==”和“<”。这两个都只对底层指针进行操作(在我受到惩罚之前继承了代码)。然而,在编译时,我都收到了 C2244 错误。我仅介绍相等运算符的情况,因为另一个几乎完全相同:

错误 4 错误 C2244:'SmartRef::operator ==':无法将函数定义与现有声明匹配 q:\projs\ha\src\SmartRef.cpp 114

以下代码在 VC4.2 中工作正常(我正在移植到VC2008...勇气)。

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;

    //@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;
}

我的第一个想法是有问题,因为在专门的类中没有进行比较,但后来我意识到我正在比较指针而不是对象。然后我想也许我需要对此非常明确,所以我将比较的两边都与“void*”进行比较,但这没有什么区别。所以现在我认为这个定义在 VC 2008 中无效是有原因的。但是我一辈子都无法弄清楚这个定义有什么问题。

非常感谢任何帮助。 谢谢, 丹尼斯.

*编辑:*我很尴尬,它是如此简单。我在定义中遗漏了一个常量。谢谢大家,我不知道我怎么没发现它!奖品颁发给第一个正确答案的人。

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;

    //@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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一张白纸 2024-11-03 07:22:58

中省略了函数中的 const

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

您在 SmartPtr.cpp Works

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

孤独岁月 2024-11-03 07:22:58

您在定义中缺少 const

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

另外,您应该像您一样在 .cpp 的标头中定义成员函数 - #include 对可读性没有多大作用。

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.

两相知 2024-11-03 07:22:58

模板不属于 .cpp 文件,它们也属于标头,因为编译器需要知道完整的代码来实例化它的模板。然后,您在 operator== 的定义中缺少 const。此外,所有比较运算符都应返回 bool,而不是 int

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文