问题重载运算符 c++
Issue overloading the operator c++
我不断收到以下错误
错误(主动)E0349 没有运算符“<<”匹配这些操作数
错误 C2678 二进制“<<”:未找到采用 'std::ostream' 类型左操作数的运算符(或没有可接受的转换)
我知道问题是当我尝试使运算符超载时出现错误或丢失,我认为此时它有点小,但我不知道了。任何帮助将不胜感激。
.h 文件
#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 文件
#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);
内部超载 operator<<
。这是一个问题,因为 account::display
returns 一个 std::ostream
并且没有 operator<<
的重载将 std::ostream
作为参数。
方法一
要解决这个你可以为class里面的重载operator<<
添加一个朋友声明和将 operator<<
的实现更改为如下所示:
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;
}
方法二
解决此问题的另一种方法是将 stream << Account.display(stream);
替换为 return Account.display(stream);
,如下所示:
std::ostream& operator<<(std::ostream& stream,account& Account) {
return Account.display(stream); //changed this
}
我不断收到以下错误
错误(主动)E0349 没有运算符“<<”匹配这些操作数 错误 C2678 二进制“<<”:未找到采用 'std::ostream' 类型左操作数的运算符(或没有可接受的转换)
我知道问题是当我尝试使运算符超载时出现错误或丢失,我认为此时它有点小,但我不知道了。任何帮助将不胜感激。
.h 文件
#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 文件
#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);
内部超载 operator<<
。这是一个问题,因为 account::display
returns 一个 std::ostream
并且没有 operator<<
的重载将 std::ostream
作为参数。
方法一
要解决这个你可以为class里面的重载operator<<
添加一个朋友声明和将 operator<<
的实现更改为如下所示:
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;
}
方法二
解决此问题的另一种方法是将 stream << Account.display(stream);
替换为 return Account.display(stream);
,如下所示:
std::ostream& operator<<(std::ostream& stream,account& Account) {
return Account.display(stream); //changed this
}