2014-09-23 93 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

與你如何傳遞你的論點沒有任何關係。

distvector<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

按照自己的定義,DIST爲int的載體的載體,由此DIST [i]是一個向量int因此dist [i] [j]是一個int。你不能回推一個int。