0

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; } 
2
  • 1
    stream << Account.display(stream); doesn't look right, since the return value of Account.display(stream) is, itself, an ostream& and there isn't an overload for ostream << ostream. It looks like you just want Account.display(stream); return stream;. Or possibly even just return Account.display(stream); Commented May 14, 2022 at 4:54
  • 1
    Better to avoid using namespace std - see here stackoverflow.com/questions/1452721/…. Commented May 14, 2022 at 4:56

1 Answer 1

1

The problem is that you've used:

stream << Account.display(stream); 

inside the overloaded operator<<. This is a problem because account::display returns a std::ostream and there is no overload of operator<< that takes a std::ostream as a parameter.

Method 1

To solve this you can add a friend declaration for the overloaded operator<< inside the class and change the implementation of operator<< to as shown below:

class account { //other members as before //friend declaration for overloaded operator<< friend std::ostream& operator<<(std::ostream& stream, account& Account); }; //implementation of operator<< std::ostream& operator<<(std::ostream& stream,account& Account) { stream << Account.acct_ID<<" "<< Account.acct_name<<" "<<Account.acct_balance<<" "<<Account.acct_ID; return stream; } 

Working demo

Method 2

Another way to solve this is to replace stream << Account.display(stream); with return Account.display(stream); as shown below:

std::ostream& operator<<(std::ostream& stream,account& Account) { return Account.display(stream); //changed this } 

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need a friend declaration since display is a public member of account. Seems simpler to just correct the usage of display in the operator<< overload. And also makes it a little easier to adapt this to a situation where, say, account is actually supposed to be a polymorphic base class and display can be made virtual but operator<< the free function cannot.
@NathanPierson Note that in my modified example, i have not used display. Also, since there is a overloaded operator<< for account, display is no longer needed for the given example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.