繁体   English   中英

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

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

这是家庭作业-我在为Customer.h文件定义输入>>运算符重载时遇到问题,其中来自另一个类的对象是该类的成员。 我将使用输入运算符从文本文件读取数据并将其输出到BinaryTree。 我们有一个Customer类和一个Address类。 地址是客户的成员(客户的构造函数的一部分)。 我必须阅读包含以下内容的文件:CustomerID CustomerName AddressStreet AddressCity AddressZip

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

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

};

Customer.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指针。 您确实应该使用引用或智能指针。 同样为了清晰/易读,我认为通常最好将变量名放在函数声明以及定义中,即使不需要它也是如此。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM