【问题标题】:C++ Structure/Vector Compile IssuesC++ 结构/向量编译问题
【发布时间】:2016-03-24 00:43:00
【问题描述】:

我遇到了以下我正在努力修复的编译错误:

错误 C3867: 'std::vector>::at': 函数调用缺少参数列表;使用 '&std::vector>::at' 创建指向成员的指针

我的代码是:

struct coord_type {
    int x;
    int y;
};

struct elements_type {
    vector <coord_type> my_coord;
    coord_type item2;
    coord_type item3;
};

而出现错误的函数是:

void position_time(data_type data)
{
    int        i;
    coord_type position;

    for (i = data.elements.my_coord.size() - 1; i > 0; i--) {
        position = data.elements.my_coord.at[i];
    }
}

知道如何解决这个问题吗?

【问题讨论】:

  • .at 是一个使用 .at(i) 代替的函数。

标签: c++ vector struct reference


【解决方案1】:

at 是一个函数,它以你想要的数据的索引作为参数。你有两个选择:

  • [i]改成(i)(因为它是一个函数)
  • 使用传统数组表示法访问索引 (data.elements.my_coord[i])

【讨论】:

    【解决方案2】:

    如果要遍历vector,最好用iterator,这里用reverse iterator比较好。

    void position_time(data_type data)
    {
        coord_type position;
        for_each(data.elements.my_corrd.rbegin(), data.elements.my_corrd.rend(), [&](coord_type& ct){position = ct;});
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-22
      • 2015-11-19
      • 1970-01-01
      • 2016-02-16
      • 2010-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      相关资源
      最近更新 更多