简体   繁体   English

cygwin中的C ++错误; 没有匹配的函数可以调用'std :: basic_fstream ...'

[英]C++ error in cygwin; no matching function for call to 'std::basic_fstream…'

I'm using cygwin on Windows 10. When I try to run a very simple program, one that displays lines from a file to the command prompt, I get this error: 我在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*’

I thought it was a local install issue but I installed cygwin on a second computer and the same thing happened. 我以为这是本地安装问题,但是我在第二台计算机上安装了cygwin,并且发生了同样的事情。 I'm at a loss. 我很茫然。

Sidenote; 边注; when I included the ifstream header, the error changed to "Fatal error: ifstream: No such file or directory. 当我包含ifstream标头时,错误更改为“致命错误:ifstream:没有这样的文件或目录。

Here is my code: 这是我的代码:

#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;
}

Any help would be much appreciated! 任何帮助将非常感激! This is my first post here, so if I committed any faux pas please let me know. 这是我在这里的第一篇文章,因此,如果我有任何虚假行为,请告诉我。

It looks like you need to #include <string> : 看来您需要#include <string>

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
...

The string include is added automatically by some compilers, but it appears that this is not one of them. string include是由某些编译器自动添加的,但看来这不是其中之一。

it says clear from the complier: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char* ' 它从编译器中清楚地表明: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char* '

so you have to use c string rather than std::string 所以你必须使用c string而不是std::string

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c ++中文件处理中的错误-没有匹配函数可调用`std :: basic_fstream <char, std::char_traits<char> &gt; :: open(const char [8],bool)&#39; - error in file handling in c++--no matching function for call to `std::basic_fstream<char, std::char_traits<char> >::open(const char[8], bool)' 错误C2039:“打开”:不是“ std :: basic_fstream”的成员 - error C2039: 'Open' : is not a member of 'std::basic_fstream std :: basic_fstream :: put()无效 - std::basic_fstream::put() has no effect 错误:从&#39;const char [10]&#39;转换为非标量类型&#39;std :: fstream {aka std :: basic_fstream <char> }&#39;请求 - error: conversion from 'const char [10]' to non-scalar type 'std::fstream {aka std::basic_fstream<char>}' requested std :: basic_fstream和std :: unique_lock的接口设计 - Interface designs of std::basic_fstream and std::unique_lock 为什么是 std::basic_fstream<unsigned char> 不会工作? - Why std::basic_fstream<unsigned char> won't work? std::basic_fstream<unsigned char> 不适用于 Linux - std::basic_fstream<unsigned char> doesn't work on Linux 与&#39;operator &lt;&lt;&#39;不匹配(操作数类型为&#39;std :: fstream {aka std :: basic_fstream <char> }”和“单词”) - no match for 'operator<<' (operand types are 'std::fstream {aka std::basic_fstream<char>}' and 'Word') C++ 错误:没有匹配的函数调用&#39;std::__cxx11::basic_string<char> ::附加<int> (int, int)&#39; - c++ error: no matching function for call to ‘std::__cxx11::basic_string<char>::append<int>(int, int)’ 没有匹配的函数来调用&#39;std :: basic_string <char> :: basic_string C ++ - no matching function for call to 'std::basic_string<char>::basic_string c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM