Home > Enterprise >  Issue overloading the operator c
Issue overloading the operator c

Time:05-14

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;
}

CodePudding user response:

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

  • Related