0

I am new to C++ and I can't seem to figure out overloading the I/O operators. I have read:

but I, unfortunately, can't get it right. The code I have so far is below:

#include <iostream>
#include <string>

// Sales_data structure
struct Sales_data {
  std:: string bookNo;
  unsigned units_sold = 0;
  double revenue = 0.0;
};

// logic to determine if sales data are not equal
bool operator!=(const Sales_data& data1, const Sales_data& data2) {
  // boolen comparision to produce not equal
  return data1.bookNo != data2.bookNo;
}

ostream& operator<< (ostream &out, Sales_data &cSales_data) {
  out << "(" << cSales_data.bookNo << " " << cSales_data.units_sold
      << " " << cSales_data.revenue << ")";
  return out;
}

int main() {
  Sales_data books;  // books is of type sales_data uninitialized
  double price = 0;  // price is of int type initialized at 0
  for (int i = 0; i >= 0; ++i) {
    while (std::cin >> books.bookNo >> books.units_sold >> price) {
      if (books != Sales_data()) {
        i += 1;
        // there is other code here but not relevant to the problem.
        std::cout << books << std::endl;
      }
    }
  }
  return 0;
}

The error I am obtaining is

 error: ‘ostream’ does not name a type
 ostream& operator<< (ostream &out, Sales_data &cSales_data) {
 ^
exercise2_41a.cpp: In function ‘int main()’:
exercise2_41a.cpp:52:22: error: cannot bind ‘std::ostream {aka
std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
         std::cout << books << std::endl;

The code I am having trouble with is

ostream& operator<< (ostream &out, Sales_data &cSales_data) {
  out << "(" << cSales_data.bookNo << " " << cSales_data.units_sold
      << " " << cSales_data.revenue << ")";
  return out;
}

but I am not quite sure what I need to do to achieve desired results. What am I missing? I believe I am on the right track but that could be a farce as well.

Community
  • 1
  • 1
dustin
  • 4,309
  • 12
  • 57
  • 79
  • @Luca I will give it a shot but it wasn't in the example at learncpp. – dustin Apr 25 '15 at 02:29
  • @Luca that produces: `declaration of ‘operator<<’ as non-function std::ostream& operator<< (ostream &out, Sales_data &cSales_data) {` with a litany of other errors – dustin Apr 25 '15 at 02:31
  • 2
    Make `cSales_data` a `const Sales_data &`. As it is, callers will have to jump through hoops to pass a const object when there's no reason they should have to. – chris Apr 25 '15 at 02:31
  • @chris so it should read `&const Sales_data` or no ampersand? – dustin Apr 25 '15 at 02:32
  • Try making the function argument `std::ostream` as well, and `const Sales_data &cSales_data` pointed out by @chris . – Luca Apr 25 '15 at 02:33
  • @dustin, The code I gave is exactly what I mean. – chris Apr 25 '15 at 02:35
  • @chris `std::ostream& operator<< (ostream &out, const Sales_data &cSales_data)` produces `error: declaration of ‘operator<<’ as non-function std::ostream& operator<< (ostream &out, const Sales_data &cSales_data) {` with just `ostream&...`, the error is `error: ‘ostream’ does not name a type ostream& operator<< (ostream &out, const Sales_data &cSales_data) { ^`. I don't think I understand what you mean. – dustin Apr 25 '15 at 02:40
  • **Try something simpler.** – Beta Apr 25 '15 at 02:42
  • I didn't claim it would solve your compiler error (and there are other comments pertaining to that), but it's a good habit to get into. – chris Apr 25 '15 at 02:44
  • I'm voting to close this question as off-topic because it pertains to a very specific and detailed example of a very simple problem. The cause of the problem is unrelated to the question's title, so the question is unlikely to be of help to future readers. This could have been avoided had the author prepared a [minimal complete example](http://stackoverflow.com/help/mcve) of the problem. – Beta Apr 25 '15 at 02:53
  • @Beta simple is relative to experience. I understand it may be simple to you but that isn't the case for every individual learning the language for the first time. – dustin Apr 25 '15 at 03:00
  • Perhaps you misunderstand me. The problem is one that could appear in a `HelloWorld` program; you could remove almost all of this code (with its advanced features), paring the example down to six or seven lines, and still get the same error for the same reason. Whatever your level of experience, the problem is far simpler than the example. – Beta Apr 25 '15 at 03:08

2 Answers2

2

std::ostream& operator<< (std::ostream &out, const Sales_data &cSales_data)

Luca
  • 322
  • 1
  • 13
0

Replace all instances of ostream in your function with std::ostream. They are different, and the latter is what you need.

Optionally, make the second argument of the operator<<() accept a const reference.

Peter
  • 35,646
  • 4
  • 32
  • 74