2014-09-23 2 views
-1

вот мой код:C++ «Запрос члена '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’ 

Я думаю, что это что-то делать с переходом по ссылке, но я не уверен, что. Спасибо за вашу помощь!

ответ

2

Ничего не связано с тем, как вы передаете свой аргумент.

dist является vector<vector<int> >

является vector<int>

dist[i][j] является int, который вы звоните operator[] на. Это не сработает.

Я считаю, что вы хотите dist[i][j] = 0;

1
dist[i][j].push_back(0); 

dist[i][j] имеет тип INT, он не имеет push_back функции члена.

Это зависит от того, что вы действительно хотите сделать, простое изменение может быть:

dist[i][j] = 0; 
0

Согласно вашему определению, расстояние вектор вектора междунар, таким образом, расстояние [я] является вектором int, поэтому dist [i] [j] является int. Вы не можете нажать на int.

Смежные вопросы