I keep getting the following errors
Error (active) E0349 no operator "<<" matches these operands Error C2678 binary '<<': no operator found which takes a left-hand operand of type 'std::ostream' (or there is no acceptable conversion)
I know the issue is that there's something going wrong or missing when I try to overload the operator and I think its something small at this point, but I have no idea anymore. Any help would be appreciated.
.h file
#include <iostream> #include <string> #include <ostream> using namespace std; #ifndef ACCOUNT_H #define ACCOUNT_H class account { private: int acct_ID; string acct_name; float acct_balance; static int next_id; public: account(); account(account& rhs); ~account(); account(int acct_ID, string acct_name, float acct_balance); void set_ID(int acct_ID); int get_ID()const; void set_name(string acct_name); void input(); void set_balance(float acct_balance); ostream& display(ostream& stream); }; ostream& operator<<(ostream& stream, account& Account); //!!!!!!!!!!!!!!!!!!! #endif .cpp file
#include "Account.h" account::account(): acct_ID{0},acct_name{0},acct_balance{ 0 }{} account::account(account& rhs): acct_ID{ rhs.acct_ID }, acct_name{ rhs.acct_name }, acct_balance{ rhs.acct_balance }{} account::account(int ID, string name, float balance) :acct_ID{ ID }, acct_name{ name }, acct_balance{ balance } { } int account::next_id = 0; account::~account() {} void account::set_ID(int ID) { acct_ID = ID; } int account::get_ID()const { int acct_ID = 0; return acct_ID; } void account::set_name(string name) { acct_name = name; } void account::input() { cout << "Enter the name: "; cin >> acct_name; float x; cout << "Enter the balance: "; cin >> x; acct_balance += x; acct_ID = next_id; next_id++; } //!!!!!!!!!!!!!!!!!!!!!!! ostream& account::display(ostream& stream) { stream << "Account name: " << this->acct_name; stream << "Account balance: " << this->acct_balance; stream << "Account ID: " << this->acct_ID; return stream; } ostream& operator<<(ostream& stream,account& Account) { stream << Account.display(stream); return stream; } //!!!!!!!!!!!!!!! void account::set_balance(float balance) { acct_balance = balance; }
stream << Account.display(stream);doesn't look right, since the return value ofAccount.display(stream)is, itself, anostream&and there isn't an overload forostream << ostream. It looks like you just wantAccount.display(stream); return stream;. Or possibly even justreturn Account.display(stream);using namespace std- see here stackoverflow.com/questions/1452721/….