结构的c ++向量,为什么我不能使用vector.at(i)或vector [i]来构造struct?

时间:2021-07-04 06:52:41
struct info{
  int num;
  int weight;
};
vector<info> nbr;

the above declarations are global, and I have a loop in my function that looked like this

上面的声明是全局的,我的函数中有一个循环,看起来像这样

for(int i=0;i<nbr.size();i++){
  info i = nbr.at(i);
  vector<int> v;
  v.push_back(i.weight);
  tb[i.num] = v;
}

info i = nbr.at(i) gives me error: no matching function for call to 'std::vector<info, std::allocator<info> >::at(info&)'

info i = nbr.at(i)给出错误:没有匹配函数来调用'std :: vector >> :: at(info&)' ,std>

why? what did I do wrong?

为什么?我做错了什么?

1 个解决方案

#1


4  

for(int i=0;i<nbr.size();i++){
    info i = nbr.at(i);

This is why. You're redefining what i refers to.
It's both your loop index int i and info i, and your compiler is wondering why you're passing an info& to vector::at.

这就是为什么。你正在重新定义我所指的东西。这是你的循环索引int i和info i,你的编译器想知道为什么你要传递一个信息&vector :: at。

Change the name and you should be good to go.

改变名称,你应该好好去。

#1


4  

for(int i=0;i<nbr.size();i++){
    info i = nbr.at(i);

This is why. You're redefining what i refers to.
It's both your loop index int i and info i, and your compiler is wondering why you're passing an info& to vector::at.

这就是为什么。你正在重新定义我所指的东西。这是你的循环索引int i和info i,你的编译器想知道为什么你要传递一个信息&vector :: at。

Change the name and you should be good to go.

改变名称,你应该好好去。