io - C++: overloading the I/O operators -
i new c++ , can't seem figure out overloading i/o operators. have read:
- overloading operator<<: cannot bind lvalue ‘std::basic_ostream<char>&&’
- overloading operator<<: cannot bind ‘std::basic_ostream<char>’ lvalue ‘std::basic_ostream<char>&&’
- std::ostream {aka std::basic_ostream<char>} ivalue 'std::basic_ostream<char>&&
- overloading i/o operators on learn cpp
but i, unfortunately, can't right. code have far below:
#include <iostream> #include <string> // sales_data structure struct sales_data { std:: string bookno; unsigned units_sold = 0; double revenue = 0.0; }; // logic determine if sales data not equal bool operator!=(const sales_data& data1, const sales_data& data2) { // boolen comparision 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 of type sales_data uninitialized double price = 0; // price of int type initialized @ 0 (int = 0; >= 0; ++i) { while (std::cin >> books.bookno >> books.units_sold >> price) { if (books != sales_data()) { += 1; // there other code here not relevant problem. std::cout << books << std::endl; } } } return 0; } the error obtaining is
error: ‘ostream’ not name 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 ‘std::basic_ostream<char>&&’ std::cout << books << std::endl;
the code having trouble
ostream& operator<< (ostream &out, sales_data &csales_data) { out << "(" << csales_data.bookno << " " << csales_data.units_sold << " " << csales_data.revenue << ")"; return out; } but not quite sure need achieve desired results. missing? believe on right track farce well.
std::ostream& operator<< (std::ostream &out, const sales_data &csales_data)
Comments
Post a Comment