I am new to C++ and I can't seem to figure out overloading the I/O operators. I have read:
- Overloading operator<<: cannot bind lvalue to ‘std::basic_ostream<char>&&’
- Overloading operator<<: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
- std::ostream {aka std::basic_ostream<char>} Ivalue to 'std::basic_ostream<char>&&
- Overloading the I/O operators on Learn cpp
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.