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

Errors when trying to cout an object from a binary tree

本文关键字:对象 出错 二叉树      更新时间:2023-10-16

好吧,所以每当我试图运行它时,我都会遇到错误。

"错误C2679:二进制'<<':找不到接受'Customer'类型的右侧操作数的运算符(或者没有可接受的转换(">

这与过载<lt;操作员错了?

这是我的二叉树类的函数

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
void Customer::operator << (const Customer &right)
{
    print();
}

确实是错误的。这些操作符将被"注入"到流类中,声明如下:

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绑定。这些运算符应可用于任何流。

您应该将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); }