我在某些 I/O 代码上收到错误 C2664
void BinaryTree::InitializeFromFile(string Filename){
ifstream inFile;
inFile.open(Filename, fstream::binary);
if(inFile.fail()){
cout<<"Error in opening file "<<Filename;
return;
}
for(int i=0;i<=255;i++) Freq[i]=0;
char c;
inFile.get(c);
while(!inFile.eof()){
Freq[c] ++;
inFile.get(c);
}
}
HuffmanTree.cpp(293) : error C2664: 'void std::basic_ifstream<_Elem,_Traits>::
open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1
from 'std::string' to 'const wchar_t *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called
第 293 行是 inFile.open(Filename, fstream::binary);
void BinaryTree::InitializeFromFile(string Filename){
ifstream inFile;
inFile.open(Filename, fstream::binary);
if(inFile.fail()){
cout<<"Error in opening file "<<Filename;
return;
}
for(int i=0;i<=255;i++) Freq[i]=0;
char c;
inFile.get(c);
while(!inFile.eof()){
Freq[c] ++;
inFile.get(c);
}
}
HuffmanTree.cpp(293) : error C2664: 'void std::basic_ifstream<_Elem,_Traits>::
open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1
from 'std::string' to 'const wchar_t *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called
Line 293 is inFile.open(Filename, fstream::binary);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
Filename.c_str()
代替 -open()
不采用std::string
作为文件名参数。Use
Filename.c_str()
instead -open()
doesn't take astd::string
as a parameter for the filename.在调用
ifstream::open
时使用Filename.c_str()
代替Filename
use
Filename.c_str()
instead ofFilename
in the call ofifstream::open
有点令人困惑的是,
ifstream::open
采用 C 字符串,而不是 C++std::string
。将行更改为:我不知道为什么 C++ 标准库的设计者做出这样的选择,但是就是这样。
Somewhat perplexingly,
ifstream::open
takes a C-string, not a C++std::string
. Change the line to:I have no idea why the designers of the C++ standard library made this choice, but there you go.
ifstream
构造函数需要一个const char *
。使用Filename.c_str()
。The
ifstream
ctor expects aconst char *
. UseFilename.c_str()
.