尝试从二叉树中输出对象时出错

时间:2014-04-29 02:37:24

标签: c++ object operator-overloading binary-tree

好的,所以每当我尝试运行时,我都会收到错误。

"错误C2679:二进制'<<' :找不到采用类型'客户'的右手操作数的运算符。 (或者没有可接受的转换)"

这是否与重载的<<有关操作员错了吗?

这是我的二叉树类

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

这是我的Customer类(重载在最后实现)

#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

2 个答案:

答案 0 :(得分:0)

void Customer::operator << (const Customer &right)
{
    print();
}
确实是错的。那些操作员应该被注入&#34;使用类似此声明的内容进入流类:

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

然后用以下内容定义它:

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

对于您的具体情况,它可能是:

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

请注意,您的print()会员功能对于广义打印无效,因为它与cout相关联。这些运算符应该可用于任何流。

答案 1 :(得分:0)

您应该将operator<<声明为非成员函数,因为ostream将被视为operator<<的第一个参数,用户定义类型的成员函数无法满足它。例如:

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); }