-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,351
  • 1
  • 18
  • 43
  • 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 `<<` operators in the same namespace as the user defined type. It may be the global namespace here, but that's certainly not always the case, as your comment "Do the same for `::operator>>` would suggest. – James Kanze Jul 26 '13 at 15:05
  • And of course, `operator>>` is generally an order of magnitude more difficult than `operator<<`. Something you should think about in `operator<<`: what happens, for example, if `wordchecklist::name` can contain spaces. – 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<<` outputs, then one can simply use `std::getline()` to read one line then parse it. –  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 `<<` operator always outputs a full line, including the trailing `'\n'`. This is not the usual convention. And you'd still have to parse the line you read. Anytime you output strings, you must establish some sort of convention it order to delimit them. In the simplest case, the strings cannot contain spaces, and your code would work; but most of the time, you'll need some sort of delimiters, or something to ensure that you can find the end of each string. – 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 `<<` operator always outputs a full line" - isn't that **exactly** what his requirement ("mywordchecklistname mywordchecklisttype mywordcheckliststats") suggests? –  Jul 26 '13 at 15:39
  • @H2CO3 Not really. And your `operator<<` doesn't output a new line. But as an example, how would you write and read `wordchecklist data[2] = { { "James A.", "Kanze", 42 }, { "James", "A. Kanze", 24 } };`? There's a lot more work upfront if you want to do IO correctly. (Admittedly, that wasn't his question, and maybe he realizes this. But IMHO, it's worth at least mentioning.) – 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