无法理解我的错误意味着什么 [英] Couldn't understand what my error means
问题描述
我收到了这个错误:
错误C2664:''Vector :: setVector'':无法将参数1从''const std :: ifstream''转换为''std :: ifstream''
怎样它无法从std :: ifstream转换为std :: ifstream ???
这里是相关代码(我没有把所有变量都放在那些不相关的地方):
头文件:
class Matrix {
public:
。
。
。
。
void setMatrix(ifstream txtFile);
};
void Matrix :: setMatrix (ifstream txtFile){
int x;
for(int i = 0; i< this-> rowLength; i ++){
for(int j = 0; j< this-> columnLength; j ++){
txtFile>> x;
this-> contents [i] [j] = x;
}
}
}
cpp文件:
void main(){
string txtFileName;
Matrix matrix1;
cout< < 输入矢量/矩阵文件的名称(使用txt文件):" ;;
cin>> txtFileName;
ifstream txtFile(txtFileName.c_str());
matrix1.setMatrix(txtFile); //错误发生在这里
}
I got this error:
error C2664: ''Vector::setVector'' : cannot convert parameter 1 from ''const std::ifstream'' to ''std::ifstream''
How can it not be able to convert from std::ifstream to std::ifstream???
Here''s the relevant code (I didn''t put all the variables as those are not relevant):
header file:
class Matrix{
public:
.
.
.
.
void setMatrix(ifstream txtFile);
};
void Matrix::setMatrix (ifstream txtFile) {
int x;
for (int i = 0; i < this->rowLength; i++) {
for (int j = 0; j < this->columnLength; j++) {
txtFile >> x;
this->contents[i][j] = x;
}
}
}
cpp file:
void main() {
string txtFileName;
Matrix matrix1;
cout << "Enter name of vector/matrix file (use a txt file): ";
cin >> txtFileName;
ifstream txtFile(txtFileName.c_str());
matrix1.setMatrix (txtFile); //error occurs here
}
推荐答案
我发布了错误的错误代码,这里是相关的错误代码:
错误C2664:''Matrix :: setMatrix'':无法将参数1从''std :: ifstream''转换为''std :: ifstream''
没有复制构造函数可用于类''std :: basic_ifstream< _Elem,_Traits>''或构造函数尝试非法转换为非-__ gc引用
with
[
_Elem = char,
_Traits = std :: char_traits< char>
]
I posted the wrong error code, here''s the relevant error code:
error C2664: ''Matrix::setMatrix'' : cannot convert parameter 1 from ''std::ifstream'' to ''std::ifstream''
No copy constructor available for class ''std::basic_ifstream<_Elem,_Traits>'' or constructor attempts to perform illegal conversion to non-__gc reference
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
有你尝试通过参考传递它:
void Matrix :: setMatrix(const ifstream& txtFile);
??
PS:请使用代码标签(#button)
Savage
Have you tryed passing it with reference:
void Matrix::setMatrix(const ifstream &txtFile);
??
PS:Please use code tags(#button)
Savage
感谢您的回复,
如果我通过引用传递,
in main();
我应该使用:
matrix1.setMatrix(txtFile); //这里没有编译错误,但返回的值是内存地址,而不是实际值
或
matrix1.setMatrix (安培; txtFile); //这给出了编译错误
错误C2664:''Matrix :: setMatrix'':无法将参数1从''std :: ifstream * __ w64''转换为''std :: ifstream&''
thanks for replying,
if I pass by reference,
in main();
should i use:
matrix1.setMatrix (txtFile); //there''s no compile error here but the values returned are memory addresses, not the actual values
or
matrix1.setMatrix (&txtFile); //this gives a compile error
error C2664: ''Matrix::setMatrix'' : cannot convert parameter 1 from ''std::ifstream *__w64 '' to ''std::ifstream &''
这篇关于无法理解我的错误意味着什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!