user3434233
user3434233

Reputation: 19

How to convert from type (Class) to std:string?

So I have an array of strings called blog[] and I'm trying to use a string from the array at position i, like:

outfile << blog[i];

But the problem is that blog[i] is of type MicroBlog (MicroBlog is a class I'm working with)

So my question is how can I convert from type MicroBlog to type string?

Here is the method I'm trying to use blog[i] in:

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();
        }             




}

Upvotes: 0

Views: 108

Answers (1)

yizzlez
yizzlez

Reputation: 8805

You have to write your own operator ie toString() or overload <<:

 class Microblog {

     ....

     std::string toString() const { //public
          string ret = all_the_data;
          return ret;
     }
 };

And then outfile << blog[i].toString();

Upvotes: 1

Related Questions