C++运算符重载输入运算符>>,将类外部的对象作为成员

C++ operator overloading input operator >> with object outside class as member

本文关键字:运算符 对象 成员 外部 重载 输入 C++      更新时间:2023-10-16
这是

家庭作业 - 我在为 Customer.h 文件定义输入>>运算符重载时遇到问题,其中另一个类的对象是此类的成员。 我将使用输入运算符从文本文件中读取数据并将其输出到 BinaryTree。 我们有一个客户类和一个地址类。 地址是客户的成员(客户构造函数的一部分)。 我必须读取包含以下内容的文件:客户编号客户名称地址街道地址城市地址压缩

客户的构造函数是客户(int custID,字符串custName,Address*地址),所以我在客户类中的输入重载时遇到问题,我必须在地址数据中读取街道,城市,州,邮政编码,但将其存储为客户地址。 我的班级声明可能也做错了什么。 这是我所拥有的:

客户.h:

#pragma once
#include <string>
#include "Address.h"
using namespace std;
class Customer
{
private:
//Attributes for customers
int custID;
string custName;
Address* address;
public:
//Constructors for Customer
Customer();
Customer(int, string, Address*);
~Customer();
//Setters for Customer
void setCustID(int);
void setCustName(string);
void setCustAddress(Address*);
//Getters for Customer
int getCustID() {return custID;}
string getCustName() {return custName;}
//Operator overloads
bool operator>(Customer obj) {return custID > obj.custID;}
bool operator<(Customer obj) {return custID < obj.custID;}
bool operator==(Customer obj) {return custID == obj.custID;}
//Operator overloads for input
friend istream &operator>>(istream &input, Customer &customer) {
    input >> customer.custID >> customer.custName >> /*?????  Here's where I can't figure out what to call for the address street, city, state, zip; */ << endl;
    return input;
}
//Operator overloads for output
friend ostream &operator<<(ostream &output, Customer &customer) {
    output << "CustID: " << customer.custID << endl << "Customer Name: " << customer.custName << endl << "Customer Address: " << customer.address << endl;
    return output;
}
};

客户.cpp:

#include "Customer.h"
//Customer no arg constructor
Customer::Customer()
{
custID = 0;
custName = "";
}
//Customer constructor
Customer::Customer(int custID, string custName, Address* address)
{
this->custID = custID;
this->custName = custName;
this->address = address;
}
//Customer destructor
Customer::~Customer()
{
}

地址.h:

#pragma once
#include <string>
using namespace std;
class Address 
{
private:
string street;
string city;
string state;
string zip;
public:
//Constructors for address
Address();
Address(string, string, string, string);
~Address();
//Setters for address
void setAddressStreet(string);
void setAddressCity(string);
void setAddressState(string);
void setAddressZip(string);
//Getters for address
string getAddressStreet() {return street;}
string getAddressCity() {return city;}
string getAddressState() {return state;}
string getAddressZip() {return zip;}
//Operator overload for input
friend istream &operator>>(istream &input, Address &address) {
    input >> address.street >> address.city >> address.state >> address.zip;
    return input;
}
//Operator overload for output
friend ostream &operator<<(ostream &output, Address &address) {
    output << "Street: " << address.street << endl << "City: " << address.city << endl << "State: " << address.state << endl << "Zip: " << address.zip << endl;
    return output;
}
};

地址.cpp:

#include "Address.h"
//Address no arg constructor
Address::Address()
{
street = "";
city = "";
state = "";
zip = "";
}
//Address constructor
Address::Address(string street, string city, string state, string zip)
{
this->street = street;
this->city = city;
this->state = state;
this->zip = zip;
}
//Address destructor
Address::~Address()
{
}

你应该打电话

friend istream &operator>>(istream &input, Customer &customer) {
    input >> customer.custID >> customer.custName >> (*customer.address);
    return input;
}

这将调用您在 Address 类中定义的提取运算符。

话虽如此,检查运算符是否正确阅读可能是个好主意。 我也不确定你为什么要使用原始Address指针。 您确实应该使用引用或智能指针。 同样为了清晰/可读性,我认为将变量名称放在函数声明和定义中通常是一种很好的做法,即使不需要它。