C++ "请求成员'push_back'
C++ "Request for member 'push_back'
这是我的代码:
void MyWork::computeDistances()
{
int column = sentence1.size();
int row = sentence2.size();
//int min = 0;
dist.resize(column);
for (int i = 0; i < column; i++){
dist[i].resize(row);
}
for (int i = 0; i < column; i++){
for (int j = 0; j < row; j++){
cout << "A" << endl;
if (i == 0){
if (sentence1[j] == sentence2[i]){
dist[i][j].push_back(0);
在主文件中,我已经将2D矢量声明为:
vector<vector<int> > dist;
然而,我得到了一个错误:
MyWork.cpp:30:17: error: request for member ‘push_back’ in ‘(&((MyWork*)this)->MyWork::dist.std::vector<_Tp, _Alloc>::operator[] [with _Tp = std::vector<int>, _Alloc = std::allocator<std::vector<int> >, std::vector<_Tp, _Alloc>::reference = std::vector<int>&, std::vector<_Tp, _Alloc>::size_type = unsigned int](((unsigned int)i)))->std::vector<_Tp, _Alloc>::operator[] [with _Tp = int, _Alloc = std::allocator<int>, std::vector<_Tp, _Alloc>::reference = int&, std::vector<_Tp, _Alloc>::size_type = unsigned int](((unsigned int)j))’, which is of non-class type ‘int’
我认为这与通过引用有关,但我不确定是什么。谢谢你的帮助!
与如何传递参数无关。
dist
是vector<vector<int> >
dist[i]
是vector<int>
dist[i][j]
是一个int
,您正在调用它operator[]
。这不起作用。
我相信你想要dist[i][j] = 0;
dist[i][j].push_back(0);
dist[i][j]
的类型为int,它没有push_back
成员函数。
这取决于你真正想做什么,一个简单的改变可能是:
dist[i][j] = 0;
根据您的定义,dist是int向量的向量,因此dist[i]是int向量,因此dist[i][j]是int。您不能对int进行回退。
相关文章:
- 推导 std::vector::back() 的返回类型
- vector.back() 和 vector[vector.size() - 1] 之间的区别?
- vector.push_back(vector.back()+1) 是未定义的行为吗?
- C++自己的对象堆栈.在 push() 上复制了什么?
- C++ push() 和 pop() 方法使用指针的动态 LinkedList 的问题
- 如何像在javascript中一样"push" c ++映射
- 线路抑制状态错误 C4703 可能未初始化的局部指针变量"back"已使用
- std::vector using back(), pop_back(), push_back(), 得到'double free or corruption'错误
- vector.back()和vector.end()有什么区别
- 列表大小为 1,但 front() 和 back() 不相等
- 返回C++中没有 back() 方法的容器的最后一个元素?
- 在非空 std::list 上使用 std::list.back<int>() 时"Segmentation Fault"
- 模板堆栈类的复制构造函数中的 pop() 和 push() 不起作用
- 我们可以使用Back() - 值索引在其上执行向量插入
- 当我运行以下代码添加str.front() str.back时,它给了我200个,但为什么
- push(ing)back在循环中对象指针
- 在 C++ 中将元素添加到空向量:为什么 push.back 有效而 [] 无效
- 我似乎无法让 v.push.back() 处理字符串
- String::push_back() does not push back
- 向量resize(), push.back(), reserve()方法