C++天鹅座的错误;没有用于调用"std::basic_fstream..."的匹配函数
C++ error in cygwin; no matching function for call to 'std::basic_fstream...'
我在Windows 10上使用cygwin。当我尝试运行一个非常简单的程序时,该程序将文件中的行显示到命令提示符,出现此错误:
$ c++ -c test.cpp
test.cpp: In function ‘int main()’:
test.cpp:9:31: error: no matching function for call to ‘std::basic_fstream<char>::open(std::string&, const openmode&)’
myfile.open(filename, ios::in);
^
In file included from test.cpp:2:0:
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/fstream:1001:7: note: candidate: void std::basic_fstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
open(const char* __s,
^
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/fstream:1001:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
我以为这是一个本地安装问题,但我在第二台计算机上安装了cygwin,同样的事情发生了。我不知所措。
旁注;当我包含ifstream标头时,错误更改为"致命错误:ifstream:没有这样的文件或目录。
这是我的代码:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string filename="names.txt";
fstream myfile;
myfile.open(filename, ios::in);
string firstname, lastname, id;
for (int i = 0; i < 1; ++i)
{
myfile >> firstname >> lastname >> id;
cout << firstname << " " << lastname << id <<endl;
}
return 0;
}
任何帮助将不胜感激!这是我在这里的第一篇文章,所以如果我犯了任何失礼,请告诉我。
看起来你需要#include <string>
:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
...
包含string
是由某些编译器自动添加的,但似乎这不是其中之一。
它从编译器中清楚地说:no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*
'
所以你必须使用c string
而不是std::string
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 如何在Visual Basic中使用矩形函数OpenGL绘制矩形
- visual Basic c++构造函数表示法
- 从c++调用Visual Basic函数
- 如何从Visual Basic中调用C++构造函数