Question

Alright so whenever I try to run this I am getting an error.

"error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Customer' (or there is no acceptable conversion)"

Does this have some thing to do with the overloaded << operator being wrong?

Here's the function from my binary tree class

template <class elemType>
void binaryTreeType<elemType>::inorder(binaryTreeNode<elemType> *p) const
{
    if (p != NULL)
    {
        inorder(p->llink);
        cout << p->info << " ";
        inorder(p->rlink);
    }
}

And here's my Customer class (the overloads are implemented at the end)

#ifndef H_Customer
#define H_Customer

#include <iostream>
#include <string>
#include <fstream>
#include "Address.h"
using namespace std;

//template <class elemType>
class Customer
{
public:
void print() const;

void setNum(int num);
void setName(string tempname);
void setAddress(string street, string city, string state, string zip);

int getCustNum() const;

string getName() const;

Address getAddress() const;

Customer();

Customer(int num, string tempname);

Customer readFile(int &counter);

//void writeFile();
//void cmpname(string name1, string name2, string last1, string last2);

//inline bool operator< (const Customer& lhs, const Customer& rhs){ /* do actual comparison */ }
//inline bool operator> (const Customer& lhs, const Customer& rhs){return  operator< (rhs,lhs);}
bool operator == (const Customer &);        // Overloaded ==
bool operator < (const Customer &);
bool operator > (const Customer &);
void operator << (const Customer &);

private:
int custNum;
string name;
Address address;
};

//template <class elemType>
void Customer::print() const
{
cout << custNum << endl << name << endl;
address.print();
}

//template <class elemType>
void Customer::setNum(int num)
{
custNum = num;
}

//template <class elemType>
void Customer::setName(string tempname)
{
name = tempname;
}

//template <class elemType>
void Customer::setAddress(string street, string city, string state, string zip)
{
    address = Address(street, city, state, zip);
}

//template <class elemType>
int Customer::getCustNum() const
{
return custNum;
}

//template <class elemType>
string Customer::getName() const
{
return name;
}

//template <class elemType>
Address Customer::getAddress() const
{
//return //addddddddddddddrrrrrrrrrrrrrreeeeeeeeeeeeeessssssssssss; 
    //Address<elemType> obj = address;
    return address;
}

//template <class elemType>
//Default constructor
Customer::Customer() 
{
custNum = 0;
name = "";
address = Address();
}

//template <class elemType>
//Constructor with parameters
Customer::Customer(int num, string tempname) 
{
custNum = num;
name = tempname;
}



//template <class elemType>
Customer Customer::readFile(int &counter)
{
    int num;
    string num2;
    string name;
    string street;
    string city;
    string state;
    string zip;

    ifstream infile;

    infile.open("inputFile.txt");

    for(int i=0; i<=counter; i++){
        getline(infile, num2);
        getline(infile, name);
        getline(infile, street);
        getline(infile, city);
        getline(infile, state);
        getline(infile, zip);
    }

    num = atoi(num2.c_str());
    Customer obj = Customer(num, name);
    obj.setAddress(street, city, state, zip);

    counter++;
    infile.close();

    return obj;
}

bool Customer::operator == (const Customer &right)
{
    return custNum == right.custNum;
}

bool Customer::operator < (const Customer &right)
{
    return custNum < right.custNum;
}

bool Customer::operator > (const Customer &right)
{
    return custNum > right.custNum;
}

void Customer::operator << (const Customer &right)
{
    print();
}

/*template <class elemType>
void Customer<elemType>::cmpname(string name1, string name2, string last1, string last2)
{
    if (name1.compare(name2) == 0){
            cout << name1 << " " << last1 << " and " << name2 << " " << last2 << " have the same first name.";
    }
    if (last1.compare(last2) == 0){
            cout << name1 << " " << last1 << " and " << name2 << " " << last2 << " have the same last name.";
    }
}*/
#endif
Was it helpful?

Solution

void Customer::operator << (const Customer &right)
{
    print();
}

is indeed wrong. Those operators are meant to be "injected" into the stream classes with something like this declaration:

friend ostream& operator<< (ostream& os, const Customer& cust);

and you define it with something like:

ostream& operator<< (ostream& os, const Customer& cust)
{
    os << cust.someField; // this bit is customised per your needs.
    return os;
}

For your particular case, it would probably be something along the lines of:

os << "[custnum=" << custNum
   << ",name="    << name
   << ",address=" << address
   << "]";

Note that your print() member function is useless for generalised printing since it's tied to cout. Those operators should be available to any stream.

OTHER TIPS

You should declare operator<< as a non-member function, because ostream will be taken as the 1st argument for operator<<, a user define type's member function can't satisfy it. For example:

class Customer
{
    ...
    public:
        ostream& print(ostream &sout);
};

ostream& Customer::print(ostream &sout)
{
    sout << custNum << endl << name << endl;
    address.print(sout);  // maybe need to change the declaration of class Address too
    return sout;
}

ostream& operator<<(ostream& sout, const Customer& c) { return c.print(sout); }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top