类型错误的引用初始化无效

Invalid initialization of reference of type error

本文关键字:初始化 无效 引用 错误 类型      更新时间:2023-10-16

感谢任何愿意帮助我的人。

首先,代码:

#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éreten 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<<"hejn";
        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 ===|

首先是编译错误:

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);

并改变实施(关于垫子的使用[j][i])。

此外,您在初始化 i 时使用变量 i if (VanNemNull(&mat[i])

第二,您的来电

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

也是使用 mat 的副本完成的,因此您在 BeolVas(..) 中所做的更改将应用于该副本,并且在函数返回时将丢失。您应该将其更改为:

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

&mat[i]的类型为 vector<int>* ,并且您将其传递给一个引用vector<vector<int> >的函数。 我不明白你的代码做得如何,无法告诉你如何修复它,但现在你知道错误意味着什么了。