【问题标题】:Windows to Linux port c++ errorsWindows 到 Linux 端口 c++ 错误
【发布时间】:2015-03-24 22:17:31
【问题描述】:

我正在将一个大型 c++ 应用程序从 Windows 移植到 Linux,到目前为止,我一直在解决问题并用标准代码替换 Windows 特定的东西。

我遇到过这样开头的模板

#define IC __attribute__((inline))

template <typename object_type, typename base_type = intrusive_base>
class intrusive_ptr
{
private:
    typedef base_type base_type;
    typedef object_type object_type;
    typedef intrusive_ptr<object_type, base_type> self_type;
    typedef const object_type* (intrusive_ptr::*unspecified_bool_type) () const;

...

public:
    IC intrusive_ptr();
    IC intrusive_ptr(object_type* rhs);
    IC intrusive_ptr(self_type const& rhs);
    IC ~intrusive_ptr();
    IC self_type& operator= (object_type* rhs);
    IC self_type& operator= (self_type const& rhs);
    IC object_type& operator*() const; // original
    IC object_type* operator->() const;   // original

... 
};


#define TEMPLATE_SPECIALIZATION template <typename object_type, typename base_type>
#define _intrusive_ptr intrusive_ptr<object_type, base_type>

TEMPLATE_SPECIALIZATION
IC typename _intrusive_ptr::object_type& _intrusive_ptr::operator* () const
{
    VERIFY(m_object);
    return (*m_object);
}

TEMPLATE_SPECIALIZATION
IC typename _intrusive_ptr::object_type* _intrusive_ptr::operator->() const
{
    VERIFY(m_object);
    return (m_object);
}

我无法理解一些事情。

是什么原因

typedef base_type base_type;

GCC 存在问题,因为它“遮蔽了模板参数‘class base_type’”。显然它是有目的的,而且微软的编译器一定是允许的。

我对下面的 TEMPLATE_SPECIALIZATION 内容也有疑问,会给出类似的错误

error: prototype for ‘typename intrusive_ptr<object_type, base_type>::object_type& intrusive_ptr<object_type, base_type>::operator*() const’ does not match any in class ‘intrusive_ptr<object_type, base_type>’

&

error: candidate is: object_type& intrusive_ptr<object_type, base_type>::operator*() const

我不是最精通 c++ 的,因为它不是我的主要语言,但到目前为止我通过尝试这个移植学到了很多东西,并将继续学到很多东西。我现在有点卡在这些错误上,希望有人能提供帮助。

谢谢。

【问题讨论】:

    标签: c++ linux templates gcc typedef


    【解决方案1】:

    base_typetypedef 非常简单:让intrusive_ptr 的用户可以使用模板参数列表中使用的类型(尽管,由于该类型实际上是“私有的”,它并没有多大意义),类型在模板定义中定义。微不足道的解决方法是将代码更改为

    template <typename Object_type, typename Base_type = intrusive_base>
    class intrusive_ptr
    {
    private:
        typedef Base_type base_type;
        typedef Object_type object_type;
        // ...
    

    我认为这也解决了重载问题:似乎找不到正确重载的问题是未定义嵌套类型的后续问题。

    【讨论】:

    • 谢谢。我知道这会归结为一些简单的事情。感谢您的帮助。
    猜你喜欢
    • 2019-03-16
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 2012-08-05
    • 2018-11-10
    • 1970-01-01
    相关资源
    最近更新 更多