【问题标题】:Transferring Int Array Values to Unsigned Vector将 Int 数组值转换为无符号向量
【发布时间】:2013-11-08 11:55:18
【问题描述】:

我有一些数据以 32 位字的形式出现,它代表 32 个通道,要么触发,要么不触发。然后我想获取这些数据并将通道单独添加为 int,以便我可以找出该通道已触发的多次。

例如 4 个频道:

001001 001000

会变成:

002001

到目前为止,我不断收到此类错误:

error: no match for ‘operator=’ in ‘*(((std::vector<unsigned int, std::allocator<unsigned int> >*)(((long unsigned int)i) * 24ul)) + dat) = data2[i]’

关于如何帮助我的任何想法?谢谢=)

void binary(int convert,vector <unsigned>* dat) {
    bitset<32> bits(convert);
    //cout << bits.to_string() << endl;
    //cout << bits.to_ulong() << endl;
    char data[32];
    int data2[32];

    for(unsigned i = 0; i < 32; ++i) {
      data[i] = bits[i];
      data2[i] = data[i];
      dat[i] = data2[i]; ///ERROR WAS HERE
    }

    for(unsigned i = 32; i; --i) {
      int j;
      cout << (data2[i-1]);
      }
    cout << endl;
  }


  void SerDi() {
    vector <unsigned> dat(32);

    cout << "    Reading data from memory..." << endl;
    ValVector< uint32_t> data=hw.getNode("SerDi.RAM").readBlock(8);

    hw.dispatch();
    cout << data[0]<<endl;
    cout << data[1]<<endl;
    for (unsigned i = 2; i < 7; i++) {
      binary(data[i], &dat);
    }
    //graph(dat);
  }

【问题讨论】:

  • 错误在哪一行?您能否编辑您的问题以包含 complete 错误日志?
  • 你猜对了 =) 下次我问问题时,我会包括你问的信息 =)
  • 为了将来参考,您可能还想阅读the stackoverflow checklist

标签: c++ arrays vector int unsigned


【解决方案1】:

我在猜测这是包含问题的行:

dat[i] = data2[i];

这是因为dat 是一个指针,所以dat[i]dat 索引为普通数组而不是向量对象。要解决它,不要将其作为指针传递,而是通过引用传递:

void binary(int convert,vector <unsigned>& dat) { ... }

并在没有地址运算符的情况下调用它:

binary(data[i], dat);

或者保持现在的代码,但是改变有问题的行来取消引用指针:

(*dat)[i] = data2[i];

【讨论】:

  • 谢谢,它成功了。请原谅我对这一点的了解非常少,因为指针和引用使我非常困惑。这两种方法都会永久改变向量的值吗?
  • @fiz 编译器在后台将引用作为指针处理,两者都可用于修改参数。但是,正如您现在所知,两者之间的语法完全不同。
猜你喜欢
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 1970-01-01
  • 2016-01-25
  • 2017-08-21
  • 2013-06-12
相关资源
最近更新 更多