类型错误的引用初始化无效
Invalid initialization of reference of type error
感谢任何愿意帮助我的人。
首先,代码:
#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> >
的函数。 我不明白你的代码做得如何,无法告诉你如何修复它,但现在你知道错误意味着什么了。
相关文章:
- C++-模板嵌套类的引用初始化无效
- 对全局变量的非常量引用的初始化无效
- 对类型为 std::_Bit_rerefence& 的非常量引用的初始化无效
- 模板流运算符重载错误:引用初始化无效,与basic_istream和basic_ifstream之间的差异有关
- 'inherit class'中类型 'template class' 引用的初始化无效
- 类型为"短整型 (&)"的引用初始化无效
- 类型为'std::string&的非常量引用的初始化无效,并且从类型为'std::basic_string<char>的右值
- 类型为 'cv::Point2f&' 的非常量引用的初始化无效
- 使用 boost::any_range 时非常量引用错误的初始化无效
- 指针初始化无效?如果不是,那是什么
- 引用的初始化无效:指针和值之间存在差异
- 使用具有多态性的引用时初始化无效
- 错误:非整型 'const char[]' 的静态数据成员的类内初始化无效
- 类型引用的初始化无效
- Visual C++ 2015 中右值的非常量引用初始化无效
- 类型为 std::String&- 的非常量引用的初始化无效 - 如何删除
- Cpp.中的私有静态数据成员只能在其定义时初始化,而类内初始化无效
- 错误:类型初始化无效
- 类型'int&'的引用初始化无效,传递参数 1 时出错
- 类型为'std::string&的非常量引用的初始化无效