【问题标题】:C++ operator overloading input operator >> with object outside class as memberC++ 运算符重载输入运算符 >> 以类外的对象为成员
【发布时间】:2015-04-21 16:51:29
【问题描述】:

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

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

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

【问题讨论】:

  • 我没有看到Customer 的成员address。我只看到一个成员function
  • 是的,你是对的,我添加了那些参数,以为我会使其成为构造函数,但这也不对。我把它改成了 Address* 地址;

标签: c++ input operator-overloading


【解决方案1】:

你应该打电话

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

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

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

【讨论】:

  • @kharvey 您正尝试在ifstream 上使用&lt;&lt; 来尝试输出endl。除此之外,您的代码在我删除endl 后编译,就像我在回答中所做的那样。我没有看到您在任何地方滥用&gt;&gt;
  • 是的,也发现了这个错误!我已经走得更远了,现在只是想让它读取我的文件并输出到客户对象。似乎还不能让那部分工作,我可能只需要发布另一个问题!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 2012-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多