小编Col*_*ion的帖子

ISO C++说这些是模糊的,运算符重载

当我写一些关于基本运算符重载的代码时.我发现了这段代码,

struct MyInt {
  public:
    MyInt()  : data() { };

    MyInt(int val) : data(val) { }

    MyInt& operator++() {
      ++data;

      return (*this);
    }

    MyInt operator++(int) {
      MyInt copy = *this;
      ++data;

      return copy;
    }

    MyInt& operator+=(const MyInt& rhs) {
        data += rhs.data;
        return *this;
    }

    MyInt operator+(const MyInt& rhs) const {
      MyInt copy = *this;
      copy += rhs;

      return copy;
    }

    int data;
};
Run Code Online (Sandbox Code Playgroud)

这些都很好,直到我在课程声明后添加它

MyInt operator+(const MyInt& lhs, const MyInt& rhs)
{
  MyInt copy = lhs;
  copy.data += rhs.data; …
Run Code Online (Sandbox Code Playgroud)

c++ overloading ambiguous operator-keyword

1
推荐指数
1
解决办法
528
查看次数

标签 统计

ambiguous ×1

c++ ×1

operator-keyword ×1

overloading ×1