C++ 在c+中写入输入和输出文件+;

C++ 在c+中写入输入和输出文件+;,c++,string,function,file-io,compiler-errors,C++,String,Function,File Io,Compiler Errors,我无法编译我的代码,因为它在第16行一直告诉我“错误:调用没有匹配的函数”。有什么建议吗?假设我读取文件并将所有元音写入输出文件 #include <iostream> #include <fstream> #include <string> using namespace std; int main(){ string filename; // to hold the file name ifstream inputfile; //

我无法编译我的代码,因为它在第16行一直告诉我“错误:调用没有匹配的函数”。有什么建议吗?假设我读取文件并将所有元音写入输出文件

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
    string filename;    // to hold the file name
    ifstream inputfile; // input to a file


    // Get the file name
    cout << "Enter a file name: ";
    cin >> filename;

    // Open the file
    inputfile.open(filename); // LINE 16

    char vowel; // to store the vowels
    ofstream outputfile; // to write to the file

    // open file
    outputfile.open("vowels_.txt");

    while(inputfile.get(vowel)){
        //If the char is a vowel or newline, write to output file.
        if((vowel == 'a')||(vowel == 'A')||(vowel =='e')||(vowel =='E')||(vowel =='i')||(vowel =='I')||(vowel =='o')||(vowel =='O')||(vowel =='u')||(vowel =='U')||(vowel =='\n') && !inputfile.eof())
            outputfile.put(vowel);

    }

    inputfile.close();
    outputfile.close();



}
#包括
#包括
#包括
使用名称空间std;
int main(){
string filename;//保存文件名
ifstream inputfile;//文件的输入
//获取文件名
cout>文件名;
//打开文件
inputfile.open(文件名);//第16行
char元音;//存储元音
ofstream outputfile;//写入文件
//打开文件
outputfile.open(“元音字母_uu.txt”);
while(inputfile.get(元音)){
//如果字符是元音或换行符,则写入输出文件。
如果((元音='a')| |(元音='a')|(元音='e')|(元音='e')|(元音='i')|(元音='i')|(元音='o')|(元音='o')|(元音='o')|(元音='u')|(元音='u')|(元音='u')| |)(元音='u')| | | | | |)(元音='u')| | | | | | | | | | | |
outputfile.put(元音);
}
inputfile.close();
outputfile.close();
}
更改此选项:

inputfile.open(filename);
为此:

inputfile.open(filename.c_str());
因为
filename
是一个
std::string
,并将
const char*filename
作为参数

调用从
std::string
返回
const char*


C++11不需要这个,因为
fstream::open
被重载以获取
std::string
。使用
-std=c++11
标志编译以启用c++11



PS:(C++1之前)

第17行是一个空行。@immibis第16行,对不起,我猜是open()调用。您需要传递一个字符指针,如:
inputfile.open(filename.c_str())或编译时启用C++11或更高版本。哦,好的。谢谢你,我该怎么做?你能理解为什么不能用
std::string
?@PasserBy你是说用
fstream::open
?它与函数的原型,C++11之前的版本和post版本有关。是的,但我的意思是,最初的考虑是什么使它没有这样的重载?