尝试从二叉树中定制对象时出错
Errors when trying to cout an object from a binary tree
好吧,所以每当我试图运行它时,我都会遇到错误。
"错误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); }
相关文章:
- 在c++中访问int到类对象的映射时出错
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 按基类对象访问派生类资源时出错
- ./main:加载共享库时出错:libopencv_highgui.so.4.0:无法打开共享对象文件:没有这样的文件或
- 在 c++ 的构造函数中分配对象向量时出错
- 从主线程中询问运行对象表 (ROT) 时出错
- 加载共享库时出错:libbsoncxx.so._noabi:无法打开共享对象文件:没有此类文件或目录
- 从 Arduino 中的自定义C++库构造对象时出错
- 在 boost::heap::p riority_queue 中推送结构对象时出错
- 创建 OpenGL 对象期间出错
- 为什么在定义类之前声明类的对象会在友元类中给出错误,而在友元函数中不会出错
- 创建类对象时出错
- 尝试将对象用作类的属性时出错
- 擦除矢量中的对象时出错.如何将移动作业运算符添加到我的类中
- 编译创建 Eigen::ThreadPoolDevice 对象的代码时出错
- 尝试使用构造函数创建对象时出错
- 从模板类初始化对象时出错.参考类模板实例化
- 加载共享库时出错:无法在外部硬件上打开共享对象文件:
- 可变大小的对象可能无法初始化 尽管初始化了,但出错
- 删除时出错:表达式必须是指向完整对象类型的指针