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

时间:2015-04-21 16:51:30

标签: c++ input operator-overloading

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

客户的构造函数是Customer(int custID,string 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()
{

}

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

Address.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()
{
}

1 个答案:

答案 0 :(得分:2)

你应该打电话

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

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

话虽这么说,检查您是否在操作员中正确阅读可能是个好主意。我也不确定你为什么使用原始的Address指针。你真的应该使用引用或智能指针。同样为了清晰/可读性,我认为将变量名称放在函数声明和定义中通常是一种好习惯,即使它不是必需的。