2011-05-17 43 views
1

百萬感謝任何人,誰願意幫助我。類型錯誤引用的初始化無效

首先,代碼:

#include <iostream> 
#include <string> 
#include <fstream> 
#include <vector> 

using namespace std; 

void Beolvas (vector<vector<int> > mat); 
bool VanNemNull (const vector<vector<int> > &mat); 

int main() 
{ 
cout << "Van-e a mátrixnak olyan oszlopa, hogy a főátló alatt csak 0-át tartalmaz, és ha igen, akkor melyik az?\n" <<endl; 

    char ch; 
    do{ 
     // Adatbevitel 
     vector<vector<int> > mat; 
     Beolvas(mat); 

     //Kiértékelés 
     int i; 
     if (VanNemNull(&mat[i])) cout<<"szupiiiiii"; 

     cout<< endl << "Futtassam újra? (I/N)";cin>>ch; 
     }while (ch!='n' && ch!='N'); 
    return 0; 
} 

void Beolvas(vector<vector<int> > mat) 
{ 
    ifstream fajl; 
    bool hiba; 
    string str; 

    do{ 
     cout << "Fajl neve:"; 
     cin >> str; 
     fajl.open(str.c_str()); 
     if (hiba = fajl.fail()) 
     { 
      cout << "Nincs ilyen nevű fájl" << endl; 
      fajl.clear(); 
     } 
    }while (hiba); 

    int n; 
    fajl >> n; 
    if (n<1) 
    { 
     cout<<"Helytelen a mátrix mérete\n Kérem ellenőrizze a forrásfájlt!\n"; 
     cout<<"E megnyomásával kilép"<<endl; 

    char ex; 
    do{ 
     cin>>ex; 
     }while (ex!='e' && ex!='E');exit(0); 
    } 

    mat.resize(n); 
    for(int i=0; i<n; ++i) 
     { 
      mat[i].resize(n); 
      for (int j=0; j<n; ++j) 
      { 
       fajl >> mat[i][j]; 
      } 
     } 
    fajl.close(); 
    cout<<"A mátrix a következöképpen néz ki:"<<"\n"; 
    cout<<"Elemszáma: "; cout<<n*n<<"\n"; 
    for (int i=0; i<n; ++i) 
     { 
      for (int j=0; j<n; ++j) 
      { 
       cout<<mat[i][j]<<"\t"; 
      } 
      cout<<"\n"; 

     } 

} 

/*void Vansor (const vector<vector<int> > mat) 
{ 
    //bool l = false; 
}*/ 

bool VanNemNull (const vector<vector<int> > mat) 
{ 
    bool l = false; 
    int i=0; 
    cout<<(int)mat.size(); 
    for (int j=i+1; !l && j<(int)mat.size(); ++j) 
    { 
     cout<<mat[j][i]<<"\n"; 
     if (l) cout<<"hej\n"; 
     l = (mat[j][i]!=0); 
     if (l= true) cout<<"22"; else cout<<"11"; 

    } 
    return (l); 
} 

的主要問題(我認爲)是最後一部分。另外,我得到這些錯誤消息:

||In function `int main()':| 
|23|error: invalid initialization of reference of type 'const std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&' from expression of type 'std::vector<int, std::allocator<int> >*'| 
|9|error: in passing argument 1 of `bool VanNemNull(const std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&)'| 
||=== Build finished: 2 errors, 0 warnings ===| 
+0

除了錯誤信息,你會遇到什麼樣的問題,即這是什麼應該做的和它有什麼作用呢?另外,請爲該問題選擇一個真正的標題。 – deceze 2011-05-17 01:58:04

+0

對不起,但我現在不是最重要的東西。現在還沒睡好30個小時。 – Richard 2011-05-17 02:12:52

+0

那麼也許是時候了。 :o) – deceze 2011-05-17 02:14:35

回答

1

首先你的編譯錯誤:

vector<vector<int> > mat; 
... 
if (VanNemNull(&mat[i])) cout<<"szupiiiiii"; 

不匹配:bool VanNemNull (const vector<vector<int> > mat);

你傳遞一個vector_of_ints副本(vector_of_vector_of_int的元素)作爲參數。 你應該調用它像這樣:

if (VanNemNull(mat)) ... 

或改變功能看起來像這樣:

bool VanNemNull (const vector<int> > mat); 

,改變implemetation(關於墊[J] [I]的使用)。

另外,您還可以在不初始化i的情況下使用if (VanNemNull(&mat[i])中的變量i。

其次,您的來電

void Beolvas(vector<vector<int> > mat); 

也與墊的副本來實現,讓你在BeolVas(..)所做的更改應用於該副本和將丟失當函數返回。 您應將其更改爲:

void Beolvas(vector<vector<int> >& mat)

0

&mat[i]的類型爲vector<int>*,而你把它傳遞給需要的vector<vector<int> >的引用的功能。我不明白你的代碼做得足夠好,可以告訴你如何解決它,但是現在你知道錯誤的含義了。

相關問題