C++运算符重载输入运算符>>,将类外部的对象作为成员
C++ operator overloading input operator >> with object outside class as member
这是
家庭作业 - 我在为 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
指针。 您确实应该使用引用或智能指针。 同样为了清晰/可读性,我认为将变量名称放在函数声明和定义中通常是一种很好的做法,即使不需要它。
相关文章:
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 运算符重载 += 添加新对象
- 如何用 2 个对象过度引导运算符
- C++内联运算符重载中,请参阅当前对象
- 使用运算符"="在C++中用值初始化对象
- C++运算符重载做双 * 对象
- 如何在自定义对象的<<运算符中添加自定义前缀
- 尝试重载输出运算符时,我无法遍历对象向量
- 我重载了 << 和 = 运算符。为什么当我将一个对象分配给另一个对象并尝试打印它时,我会被打印出来?
- 在之后仍需要使用源对象时调用父移动分配运算符
- 错误:三元运算符无法在对象中正常工作"cout"
- 为什么我在声明对象数组时不能使用 -> 运算符?
- 无法使用迭代器查找类对象。(二进制 '==': 'userInfo' 不定义此运算符)
- 重载加法运算符(使用函数向对象添加字符串)
- 临时对象上的运算符重载
- 如何使用运算符+重载函数正确返回对象?
- 如何分配适合容纳 T 类型对象的缓冲区(可能过度对齐、可能有运算符 new 等)
- 重载运算符 [] 用于从对象数组中给出特定索引