0

I have a customer class as following,

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include "SimpleDate.h"

using namespace std;

class Customer {

private:
    string customerId;
    string name;
    string address;
    string city;
    string postCode;
    SimpleDate* dateLastPurchased;
    double dollarsOwed;

public:
    //Customer(string customerIdVal, string nameVal);
    Customer();
    string getCustomerId();
    string getName();
    void setAddress(string addressVal);
    string getAddress();
    void setPostCode(string postCodeVal);
    string getPostCode();
    void setCity(string cityVal);
    string getCity();
    void setDateLastPurchased(SimpleDate* date);
    SimpleDate* getDateLastPurchased();
    void addDollarsOwed(double amount);
    double getDollarsOwed();

    friend ostream& operator<< (ostream &out, Customer &cust);
    friend istream& operator>> (istream &in, Customer &cust);
};

Overloading sections in the cpp file looks as following

ostream& operator<< (ostream &out, Customer &cust)
{
    out << cust.customerId << "\t" << cust.name << "\t" << cust.address << "\t" << cust.city << "\t" << cust.postCode << "\t" << cust.dateLastPurchased->getFullDate() << "\t" << cust.dollarsOwed << "\t" << std::endl;
    return out;
}

istream& operator>> (istream &in, Customer &cust)
{
    in >> cust.customerId;
    in >> cust.name;
    in >> cust.address;
    in >> cust.city;
    in >> cust.postCode;

    string stringData;
    in >> stringData;

    std::istringstream iss(stringData);
    std::string datePart;
    int tmp;
    vector<int> dateData;

    while(std::getline(iss, datePart, '/')) {

        sscanf(datePart.c_str(), "%d", &tmp);       
        dateData.push_back(tmp);
    }

    SimpleDate* date = new SimpleDate(dateData[2], dateData[1], dateData[0]);
    cust.dateLastPurchased = date;

    string stringDollarsOwed;
    in >> stringDollarsOwed;

    sscanf(stringDollarsOwed.c_str(), "%lf", &cust.dollarsOwed);

    return in;
}

Then in my main class I try to create a customer object as following,

Customer* cust = new Customer();

    cust << customerInfo[0] << customerInfo[1] << customerInfo[2] << customerInfo[3] << customerInfo[4] << customerInfo[5] << customerInfo[6];

But when I'm compiling I get the following error,

can you please help?

Thanks.

AppManager.cpp: In member function 'Customer* AppManager::createCustomerObject(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)':
AppManager.cpp:445: error: no match for 'operator<<' in '& cust << customerInfo.std::vector<_Tp, _Alloc>::operator[] [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >](0u)'
Customer.h:46: note: candidates are: std::ostream& operator<<(std::ostream&, Customer&)
*** Error code 1
make: Fatal error: Command failed for target `Tour'
1
  • 1
    You've shown operator>>, but the error message is about operator<<. You've defined ostream &operator<<(ostream &, Customer &). You normally want Customer const & instead. Commented Oct 16, 2013 at 5:24

2 Answers 2

1

This operator:

ostream& operator<< (ostream &out, Customer &cust)

allows you do do things like

Customer c;
std::cout << c << std::endl;
std::ofstream output("customers.txt");
output << c << std::endl;

and not

Customer c;
c << x;

and definitely not

Customer* c = ...;
c << x;
Sign up to request clarification or add additional context in comments.

1 Comment

I'm trying to set the properties of the customer, customerInfo[0] is a string value
0

You seem to be inserting the customerInfo[0] etc objects into the cust object that you have created. You cannot insert one object into another like that. The insertion/extraction operator as they are overloaded can only be used with as they are with primitive datatypes, i.e. storing and displaying values in the objects.

3 Comments

I'm trying to set the properties of the customer, customerInfo[0] is a string value
If you want to read the values from the string into the object then you need to write a separate function, that can parse the string (may be your string is ',', '-', '_' etc delimited)and populate the fields of the object.
I have a function that already does that :) customerInfo is a vector of string values, I'm trying to set them in the object using >>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.