C++ c++;流(someVariable)初始化
所以我试着这样做:C++ c++;流(someVariable)初始化,c++,C++,所以我试着这样做: #include <iostream>//For cout/cin #include <fstream> //For ifstream/ofstream using namespace std; int main() { string types[] = {"Creativity", "Action", "Service"}; for(int i = 0; i < sizeof(types)/sizeof(string); i
#include <iostream>//For cout/cin
#include <fstream> //For ifstream/ofstream
using namespace std;
int main()
{
string types[] = {"Creativity", "Action", "Service"};
for(int i = 0; i < sizeof(types)/sizeof(string); i++) {
string type = types[i];
string filename = type + ".html";
ofstream newFile(filename);
//newFile << toHTML(getActivities(type));
newFile.close();
}
return 0;
}
#包括//For cout/cin
#包含//用于ifstream/ofstream
使用名称空间std;
int main()
{
字符串类型[]={“创造性”、“动作”、“服务”};
对于(int i=0;i //newFile原始的IOstream库没有采用std::string
的构造函数。支持的唯一类型是char const*
。您可以使用c_str()
从std::string
获取char const*
:
字符串文字的类型不是std::string
类型,而是char const[n]
类型,其中n
是字符串中的字符数,包括终止的空字符
< >在C++ 2011中,文件流类被改进为也采用了 STD::String 字符串。> P>原始的IoScript库没有构造函数,采用了<代码> STD::String 。唯一支持的类型是Car const */Cuth>。可以从<代码> STD::String < /C> >使用获得< <代码> char const */COD>de>c_str()
:
字符串文字的类型不是std::string
类型,而是char const[n]
类型,其中n
是字符串中的字符数,包括终止的空字符
<> P> C++中的2011个文件流类被改进,也可以采用“代码> STD::String 字符串。”我猜,你是在使用C++ 03,因此没有一个构造函数与<代码> STD::String ,只是一个与你从
std::string
一起工作的构造函数,只有一个与中获得的C样式字符串一起工作的构造函数>c_str()
。你能发布错误吗?你已经发布了两次相同的代码…@KonradRudolph-不,第二次与第一次略有不同。不仅仅是在评论中。
#include <iostream>//For cout/cin
#include <fstream> //For ifstream/ofstream
using namespace std;
int main()
{
string types[] = {"Creativity", "Action", "Service"};
for(int i = 0; i < sizeof(types)/sizeof(string); i++) {
string type = types[i];
//Attempting to add const..
const string filename = type + ".html";
ofstream newFile(filename);
//newFile << toHTML(getActivities(type));
newFile.close();
}
return 0;
}
std::string name("whatever");
std::ofstream out(name.c_str());