-3

This is a topic is covered in a generalized way in another post that i found later: Operator overloading

But to be specific, here is a small snippet of code:

class wordchecklist
{
    string name,type;
    unsigned int stats;
};

What i want to do, is overload the output operator so that the following code works

Edit: (keep in mind it contains private members so i would prefer doing so by creating a member function of some sort):

ofstream data(database.txt,ios::app);
data<<wordchecklist;
data.close();

so that my database.txt file contains:

mywordchecklistname mywordchecklisttype mywordcheckliststats

Same goes for the input operator...

Community
  • 1
  • 1
reubenjohn
  • 1,311
  • 1
  • 16
  • 41
  • You might looking for something related to http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators – usman allam Jul 26 '13 at 14:52

1 Answers1

3

You have to define a non-member function:

std::ofstream &operator <<(std::ofstream &stream, const wordchecklist &data)
{
     stream << data.name << " " << data.type << " " << data.stats << std::endl;
     return stream;
}

Do the same for ::operator >>.

ProTip: use CamelCaps or under_scores in identifiers in order your classes to have a readable name.

  • First, it's a more or less established convention to put the `>>` and the `<>` would suggest. – James Kanze Jul 26 '13 at 15:05
  • And of course, `operator>>` is generally an order of magnitude more difficult than `operator< – James Kanze Jul 26 '13 at 15:06
  • @JamesKanze 1. Right. But what if we interpret `::operator >>` as an operator in whatever namespace this is in? 2. If we assume that the input file format is the same as the one `operator< –  Jul 26 '13 at 15:06
  • I edited the question: How to handle private data members then? – reubenjohn Jul 26 '13 at 15:32
  • `std::getline` won't work unless his `< – James Kanze Jul 26 '13 at 15:35
  • @reubenjohn I believe you can `friend` it, do something like [this](http://stackoverflow.com/questions/236801/should-operator-be-implemented-as-a-friend-or-as-a-member-function). –  Jul 26 '13 at 15:38
  • @JamesKanze "unless his `< –  Jul 26 '13 at 15:39
  • @H2CO3 Not really. And your `operator< – James Kanze Jul 26 '13 at 16:19
  • @JamesKanze Appended an `std::endl` so that the operator outputs a new line. As to what to do with an array: `std::for_each(data, data + 2, [](const T &arg) { stream << arg; });` –  Jul 26 '13 at 16:46
  • @H2CO3 The code you posted still doesn't work with the input I gave. – James Kanze Jul 27 '13 at 20:55
  • @JamesKanze Excuse me, but isn't that a bit arbitrary complaint? Show your input, show what format **exactly** it is in (but wait, wouldn't it be enough if the code works with whatever input OP has!?), then I have the chance to adapt the code. –  Jul 27 '13 at 20:57
  • @H2CO3 It's not a complaint, it's a fact. I posted an example of data that cannot be reread correctly if written by your code. Try it. – James Kanze Jul 27 '13 at 21:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34308/discussion-between-h2co3-and-james-kanze) –  Jul 27 '13 at 21:05