C++ 如何从类型(类)转换为std:string?
我有一个名为blog[]的字符串数组,我试图使用位置I处数组中的字符串,如:C++ 如何从类型(类)转换为std:string?,c++,string,class,types,std,C++,String,Class,Types,Std,我有一个名为blog[]的字符串数组,我试图使用位置I处数组中的字符串,如: outfile << blog[i]; outfile您必须编写自己的操作符ietoString()或重载您将希望您的微博类具有.toString()函数。可能的重复项是否要将其转换为std::string,或者您想将其流式输出到outfile?规范的方法是重载运算符。我将其放入.h中,然后在尝试使用blog[I]的地方对代码做什么?我已经添加了我的方法,我正在尝试使用它。这个输出文件 bool Micr
outfile << blog[i];
outfile您必须编写自己的操作符ietoString()
或重载您将希望您的微博类具有.toString()函数。可能的重复项是否要将其转换为std::string
,或者您想将其流式输出到outfile
?规范的方法是重载运算符。我将其放入.h中,然后在尝试使用blog[I]的地方对代码做什么?我已经添加了我的方法,我正在尝试使用它。这个<代码>输出文件
bool MicroBlog::SaveBlog(const string filename, const MicroBlog &AnotherMicroBlog)
{
ofstream outfile;
outfile.open(filename.c_str());
num_tweets = AnotherMicroBlog.num_tweets;
for (int i=0; i < num_tweets; i++)
{
outfile << blog[i];
outfile.close();
}
}
class Microblog {
....
std::string toString() const { //public
string ret = all_the_data;
return ret;
}
};