开发者

Invalid initialization of reference of type error

A million thanks to anyone, who is willing to help me with this.

First, the code:

#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);
}
开发者_Go百科

The main problem (I think) is the last part. Also, I get these error messages:

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


First your compile-error:

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

does not match : bool VanNemNull (const vector<vector<int> > mat);

you are passing a copy of a vector_of_ints (an element of vector_of_vector_of_int) as parameter. You should either call it like this :

if (VanNemNull(mat)) ...

or change the function to look like this :

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

and change the implemetation (in regard to usage of mat[j][i]).

Also you use the variable i in if (VanNemNull(&mat[i]) whithout initializing i.

Second ,your call to

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

is also done with a copy of mat ,so the changes you make in BeolVas(..) are applied to that copy and will be lost when the function returns. you should change it to :

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


&mat[i] has type vector<int>*, and you're passing it to a function that takes a reference to avector<vector<int> >. I don't understand what your code is doing well enough to tell you how to fix it, but now you know what the error means.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜