【问题标题】:operator +' : redefinition; different type modifiers运算符 +' :重新定义;不同的类型修饰符
【发布时间】:2012-07-02 16:22:09
【问题描述】:

我有代码:

// class declaration
class Vector3D;

// class declaration and definition
class Point3D { 
    // ...

    // function declaration (only needs class declarations)
    Point3D operator+(const Vector3D &);
};

// class definition
class Vector3D {
    // ...
};

// function definition (needs class definitions)
inline Point3D Point3D::operator+(const Vector3D &vector) {
    // ...
}

但我得到错误: 'Graphic::Point3D::operator +' : 重新定义;不同的类型修饰符

【问题讨论】:

  • 从函数定义中移除内联
  • operator+ 应该是一个 const 方法。#
  • 你用的是什么编译器?编译器在哪一行发出错误?您是否错误地定义了两次函数?
  • @PolGraphic:我怀疑您发布的代码与您尝试编译的代码大不相同。错误消息表明您的实际代码中很可能存在const 放置问题,这未反映在您发布的 sn-p 中。 inline 在这里不是问题。
  • @DeadMG:对不起,我的错。我将此代码作为another question 的答案,忘记了const

标签: c++ operator-keyword redefinition


【解决方案1】:

您问题中的代码格式正确。 Visual C++ 2012 Release Candidate 接受代码没有错误(我提到这一点是因为您的错误文本与 Visual C++ 错误 C2373 的文本相同)。

您的编译器有错误,或者您在问题中提供的代码与您正在编译的代码不同。


无论如何:operator+ 不需要是成员函数。使用非成员函数(或两个,处理不同的操作数排序)会更简单:

Point3D operator+(Point3D  const& lhs, Vector3D const& rhs);
Point3D operator+(Vector3D const& lhs, Point3D  const& rhs);

如果您确实保留了 operator+ 成员函数,它们应该是 const 限定的,以便可以使用 const 限定的左侧参数调用它们:

Point3D operator+(const Vector3D &) const;
                                    ^ const required

【讨论】:

  • 是的。就是这样。非常感谢。
  • 这是 C++11 中新的/解决的问题吗?我记得关于这个问题的讨论已经发生了很多年,如果我没记错的话,一致认为在声明中指定 inline 是不必要的,即代码的原始版本是合法的。而inline什么时候变成了类型修饰符(参考错误信息)?
  • @AndreyT:实际上,你是对的。 C ++ 11 说:“内联成员函数(无论是静态的还是非静态的)也可以在其类定义之外定义,只要它在类定义中的声明或其在类定义之外的定义将函数声明为内联” (§9.3/3)。操作词:eitherinline 是一个函数说明符。 C++ 不对任何语法元素使用术语修饰符
  • @James McNellis:没错,是我自己发现的。至少在 inline 位置方面,原始 O​​P 的代码是完全正确的。如果这是问题的真正原因,那么编译器必须被破坏。
  • @AndreyT:这确实令人震惊:一个有错误的 C++ 编译器! ;-) 我正在研究这个问题;会回来报告的。
【解决方案2】:

您发布的代码没有任何问题(假设内联函数定义放置在正确的文件中,除了operator+operator+ 处缺少return 语句和可能的设计问题)。它在多个编译器上编译时没有任何报告性质的错误。

如果您的编译器在类中未声明函数inline 存在问题,则这是您的编译器中的错误。在 C++ 语言中,完全由您决定要将 inline: 放置在类中、函数定义中或两者中的位置。都是合法的,都具有相同的效果。

编译器似乎将inline 称为“类型修饰符”这一事实是另一个迹象表明它有问题。 inline 不是类型修饰符。

但是,更有可能编译器完全没问题,错误消息实际上是由其他一些错误引起的:例如,错误放置的 const 说明符,它要么未显示,要么在代码中被错误表示已发布。

【讨论】:

    【解决方案3】:

    试试这个:

    // class declaration
    class Vector3D;
    
    // class declaration and definition
    class Point3D { 
        // ...
    
        // function declaration (only needs class declarations)
        inline Point3D operator+(const Vector3D &);
    };
    
    // class definition
    class Vector3D {
        // ...
    };
    
    // function definition (needs class definitions)
    inline Point3D Point3D::operator+(const Vector3D &vector) {
        // ...
    }
    

    【讨论】:

    • inline 说明符可以继续任何声明,包括函数定义(这也是一个声明)。我希望没有编译器在谈论“函数说明符”时会说“类型修饰符”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    相关资源
    最近更新 更多