【问题标题】:How to fix binary operator如何修复二元运算符
【发布时间】:2019-05-31 16:54:28
【问题描述】:

错误 C2676 二进制 '[': 'collection::item' 未定义此运算符或转换为预定义运算符可接受的类型

我阅读了一些帖子和 Microsoft VS 网站,但不明白如何解决问题

template<typename T1, typename T2>
class collection {
private:
    class item {
    public:
        T1 item; T2 key;
    };
    unsigned int top;
    item array = new item[top];
public:
    collection& operator[](unsigned int i) {
        return array[i];
    }
    collection(int top) {
        this->top = top;
    }
    void coutarr() {
        for (int i = 0; array[i] != 0; i++) {
            cout << array[i].item << endl;
        }
    }
    void extendarray() {
        item x = new item[top*2];
        for (int i = 0; i < top; i++) {
            x[i] = array[i];
        }
        delete []array;
        swap(array, x);
        top = top*2;

    }
    void addvar(int i, T1 item, T2 key) {
        array[i].item = item; array[i].key = key; //Here 2 Errors
    }
};

如果有人能解释我该怎么做,我将不胜感激。谢谢。

【问题讨论】:

  • 这点信息很难说,但我假设您的 item 类不提供 operator= 的实现(或复制构造函数)。你能展示你的item 课程吗?
  • @JoelBodenmann item 是一个嵌套类,class item { public: T1 item; T2 key; };
  • @Keanu 此声明 item array = new item[top];无效。
  • 只是猜测:top 没有价值。这使得数组的长度充其量为零(这真的很糟糕),最坏的情况是未定义的行为。
  • 我猜item array = new item[top];应该是一个指针:item *array = new item[top];

标签: c++ visual-studio class compiler-errors declaration


【解决方案1】:

错误 C2676 二进制 '[': 'collection::item' 未定义此运算符或转换为预定义运算符可接受的类型

这是因为

item array = new item[top];

必须

item * array = new item[top];

但是当 top 有值时,您还必须在构造函数中移动初始化


除此之外,构造函数collection(int top) 得到一个无符号值是更好的collection(unsigned top) 或更好的collection(size_t top)

当属性是指针时,缺少复制构造函数、操作符 = ...

如何检查collection&amp; operator[](unsigned int i)void addvar(int i, T1 item, T2 key)中的i的有效性,当它无效时产生异常? addvar 也是一个奇怪的名字,因为它是一个set,一个add 表示大小增加了。您无法访问 const 实例上的元素,请添加 const collection&amp; operator[](unsigned int i) const

void extendarray() 中,如果初始大小为 0,则将大小加倍不会做很多 ;-)

【讨论】:

    【解决方案2】:

    只是猜测:top 已被初始化。这使得数组充其量是一个长度为零的数组(这真的很糟糕),最坏的情况是调用未定义的行为。

    【讨论】:

      【解决方案3】:

      看来您的意思至少类似于以下内容

      template<typename T1, typename T2>
      class collection {
      private:
          class item {
          public:
              T1 item; T2 key;
          };
          unsigned int top;
          item *array;
          ^^^^^^^^^^^^^
      public:
          auto & operator[](unsigned int i) {
          ^^^^
              return array[i];
          }
      
          collection(unsigned int top) : top( top ), array( new item[top]{} )
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          {
          }
      
          ~collection() { delete []array; }
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      
          collection( const collection & ) = delete;
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      
          collection & operator =( const collection & ) = delete;
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
      
          void coutarr() {
              for (int i = 0; array[i] != 0; i++) {
                  cout << array[i].item << endl;
              }
          }
          void extendarray() {
              item x = new item[top*2];
              for (int i = 0; i < top; i++) {
                  x[i] = array[i];
              }
              delete []array;
              swap(array, x);
              top = top*2;
      
          }
          void addvar(int i, const T1 &item, const T2 &key) {
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
              array[i].item = item; array[i].key = key; //Here 2 Errors
          }
      };
      

      当然你可以进一步开发代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-29
        • 2017-07-20
        • 2017-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-07
        • 1970-01-01
        相关资源
        最近更新 更多