【发布时间】:2014-06-19 16:06:56
【问题描述】:
所以我有一个名为 blog[] 的字符串数组,我试图在位置 i 处使用数组中的一个字符串,例如:
outfile << blog[i];
但问题是 blog[i] 是 MicroBlog 类型(MicroBlog 是我正在使用的一个类)
所以我的问题是如何将微博类型转换为字符串类型?
这是我尝试使用 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();
}
}
【问题讨论】:
-
你会希望你的微博类有一个 .toString() 函数。
-
您要将其转换为
std::string,还是将其流式传输到outfile? -
规范的方法是重载运算符stackoverflow.com/questions/1549930/…
标签: c++ string class types std