C++ 我无法使此字符串在C++;

C++ 我无法使此字符串在C++;,c++,string,file,C++,String,File,我不明白为什么我会在编译字符串文件名时出错;它说它“超出了范围”,我尝试使用cstring,但没有成功,我从教科书中复制了一个基本的字符串示例代码,甚至没有编译 #include <iostream> #include <fstream> #include <iomanip> #include <string> using namespace std; int main() { ifstream file1; // setting variabl

我不明白为什么我会在编译字符串文件名时出错;它说它“超出了范围”,我尝试使用cstring,但没有成功,我从教科书中复制了一个基本的字符串示例代码,甚至没有编译

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
ifstream file1;

// setting variables up for calculation
string filename;
float average, x;
float total = 0;
float count = 0;
float min = 0;
float max = 0;


//asking for file name
cout << "What is the name of the file you wish to open" << endl;
getline(cin, filename);

// opening file
file1.open(filename);
if (file1.fail())
{
cout << "Failure to open that file, please try again." << endl;
return 0;
}

//reading file 
if (file1.is_open())
{   
while (file1 >> x)
{
    if (x <= min)
    { x = min;
    }
    if (x >= max)
    { x = max;
    }
    total = total + x;
    count++;
    if (file1.eof())
    { break;
    }
}
}
// final calculations and testing 
average = (total / average);
cout << "Minimum = " <<  min << endl;
cout << "Maximmum = " << max << endl;
cout << "The total count = " << count << endl;
file1.close();
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream文件1;
//设置用于计算的变量
字符串文件名;
浮动平均,x;
浮动总数=0;
浮点数=0;
浮动最小值=0;
浮动最大值=0;
//询问文件名

在C++ 11之前,文件流只使用了 const char */COD>作为文件名。C++ 11中,对于<代码> STD::String 也有重载。如果没有重载,请使用<代码>文件名.CyString()
在调用
open
获取指向包含
filename
内容的C字符串的指针时,我可以编译代码,您遇到了哪些具体的编译错误?需要考虑三件事:
1
发布准确的错误消息。
2
正确格式化/缩进您的代码。
3
将其剥离到最下面重现此问题所需的最少代码。您是否使用c++11标志集编译?此处的代码编译时没有错误:还要注意代码的格式,以及它与您发布的代码有多大的不同。@PaulMcKenzie如果我在没有c++11的情况下编译代码,则在open中使用字符串时会出现错误。