C++ "请求成员'push_back'

C++ "Request for member 'push_back'

本文关键字:back push C++ 请求 成员      更新时间:2023-10-16

这是我的代码:

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’

我认为这与通过引用有关,但我不确定是什么。谢谢你的帮助!

与如何传递参数无关。

distvector<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进行回退。