Reputation: 71
This is homework--I am having issues defining my input >> operator overload for my Customer.h file, where an object from another class is a member of this class. I will be using the input operator to read data in from a text file and output it to a BinaryTree. We have a Customer class and an Address class. Address is a member of Customer (part of Customer's constructor). I have to read in the file which contains: CustomerID CustomerName AddressStreet AddressCity AddressZip
Customer's constructor is Customer(int custID, string custName, Address* address) so I'm having problems with my input overload in the Customer class, where I have to read in the address data as the street, city, state, zip but store it as the Customer address. I may have done something wrong with my class declarations, too. Here is what I have:
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()
{
}
Upvotes: 0
Views: 1482
Reputation: 3651
You should be calling
friend istream &operator>>(istream &input, Customer &customer) {
input >> customer.custID >> customer.custName >> (*customer.address);
return input;
}
this will call the extraction operator you defined in your Address
class.
That being said, it may be a good idea to check that you are reading correctly in your operators. I also am not sure why you are using a raw Address
pointer. You really should use a reference or a smart pointer. Also for clarity/readability, I think it is usually good practice to put variable names in your function declarations as well as your definitions, even though it is not needed.
Upvotes: 2