Getting an error C2678 with my overloaded functions

I have 2 header files one contains my personType class which works independently as I created it for a previous lab and a binarySearchTree<Type> which allows you to create and do various functions with binary search tree's like inserting, searching,and deleting. Which also works independently usings numbers and strings. But when I have combined the 2 trying to make a binary tree of type personType I am running into this problem I can't seem to figure how to fix. The entirety of my code is provided for these 2 header files.

Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)


PersonType
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <cstring>
#include <string>
#include <vector> 
#include "orderedArrayListType.h"
#include "arrayListType.h"

		using namespace std;

#ifndef     PERSON      
#define     PERSON

	  /*************************************************************
	  declare a personType class
	  ************************************************************/
	  class personType
	  {
		  friend ostream& operator<<(ostream&, personType&);
		  friend istream& operator>>(istream&, personType&);
	  public:
		  bool operator==(personType&);       // == overloading
		  bool operator<(personType&);     // > overloading
		  bool operator<=(personType&);
		  bool operator>(personType&);        // < overloading
		  personType & operator=(personType&);// = overloading
		  //nameType getName(){return name;}
		  // dateType getDate(){return birthday;}
		  void setPerson(string, int, double);                // set the full person
		  void getPerson(string&, int&, double&);             // get the full person
		  personType();                                   // default ructor
		  personType(string, int, double);                    // a ructor with parameters
		  personType(personType&);                        // copy ructor
		  ~personType();                              //destructor
	  private:
		  string name;
		  int ID;
		  double grade;
	  };
	  //overload operator<<, which is not a member of personType, but is a friend to it.
	  ostream& operator<<(ostream& os, personType& aperson)
	  {
		  os << aperson.ID << "  " << aperson.name << "  " << aperson.grade << endl;
		  return os;
	  }
	  //overload operator>>, which is not a member of personType, but is a friend to it.
	  istream& operator>>(istream& is, personType& p)
	  {
//***********************************************************************
		  if (is == cin) //this is where the error is
//***********************************************************************
		  {
			  cout << "Please enter SSN:";
			  is >> p.ID;
			  cout << "enter name ";
			  getline(is, p.name);
			  cout << "enter grade ";
			  is >> p.grade;
		  }
		  else
		  {
			  is >> p.ID;
			  getline(is, p.name);
			  is >> p.grade;
		  }
		  return is;
	  }
	  //overload == operator
	  bool personType::operator==(personType& aperson)
	  {
		  return name == aperson.name
			  || ID == aperson.ID;
	  }
	  //overload < operator
	  bool personType::operator<(personType& aperson)
	  {
		  return grade < aperson.grade;
	  }

	  //overload < operator
	  bool personType::operator<=(personType& p)
	  {
		  return *this <p || *this == p;
	  }

	  //overload > operator
	  bool personType::operator>(personType& aperson)
	  {
		  return grade > aperson.grade;
	  }
	  //copy ructor
	  personType & personType::operator=(personType & aperson)
	  {
		  if (this != &aperson)
		  {
			  name = aperson.name;
			  ID = aperson.ID;
			  grade = aperson.grade;
		  }
		  return *this;
	  }
	  //setperson
	  void personType::setPerson(string n, int I, double g)
	  {
		  name = n;
		  ID = I;
		  grade = g;
	  }
	  //getperson
	  void personType::getPerson(string &n, int &I, double &g)
	  {
		  n = name;
		  I = ID;
		  g = grade;
	  }
	  //default ructor
	  personType::personType()            // default ructor
	  {
		  name = "cat dog";
		  ID = -1111;
		  grade = 0;
	  }
	  //another ructor
	  personType::personType(string n, int I, double g) // a ructor 
	  {
		  name = n;
		  ID = I;
		  grade = g;
	  }
	  //copy ructor
	  personType::personType(personType & aperson)
	  {
		  if (this != &aperson)
		  {
			  name = aperson.name;
			  grade = aperson.grade;
			  ID = aperson.ID;
		  }
	  }
	  // desructor
	  personType::~personType()
	  {
		  cout << " Oh, I am dying ..." << endl;
	  }




#endif 


Last edited on
Binary Search Tree, I had too seperate this into 2 different posts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#ifndef H_BST
#define H_BST

#include <iostream>
#include <iomanip>
#include <cassert>
#include <cstring>
#include <string>
#include <vector> 
#include "personType.h"
using namespace std; 


template <class Type> 
class binarySearchTree;									//forward declaration

template<class Type>
class binarySearchTreeNode
{
public: 
	friend class binarySearchTree<Type>; 
	//constructor
	binarySearchTreeNode(const Type & info, binarySearchTreeNode<Type> *lp, binarySearchTreeNode<Type> *rp)
		:element(info), left(lp), right(rp){ }; 

private: 
	Type element;										//store info
	binarySearchTreeNode<Type> *left;					//left child node pointer
	binarySearchTreeNode<Type> *right;					//right child node pointer
}; 

template<class Type>
class binarySearchTree
{
public: 
	//default constructor
	explicit binarySearchTree(); 
	//copy constructor
	binarySearchTree(const binarySearchTree<Type> & rhs); 
	//destructor
	~binarySearchTree(); 

	binarySearchTreeNode<Type> * findMin() const;		//find minimum element in the tree
	binarySearchTreeNode<Type> * findMax() const;		//find maximum element in the tree
	bool find (const Type & x) const;	//find x in the tree or not
	bool isEmpty();						//check if the tree is empty or not

	void destroyTree();					//delete the tree
	void insert(const Type & x);		//insert x into the tree
	void remove(const Type & x);		//remove x from the tree

	//overload operator =
	const binarySearchTree & operator=(const binarySearchTree<Type> & rhs); 

	//tree traversal
	void inOrderTraversal();			//print tree in in-order
	void preOrderTraversal();			//print tree in pre-order
	void postOrderTraversal();			//print tree in post-order

	int treeHeight();					//get the height of the tree
	int treeNodeCount();				//get the node count (size) of the tree
	int treeLeaveCount();				//get the leave count of the tree

protected: 
	binarySearchTreeNode<Type> * root; 

	//auxillary methods
private: 
	//copy tree rhs to rt
	binarySearchTreeNode<Type> * copyTree(binarySearchTreeNode<Type> * & rt, binarySearchTreeNode<Type> *rhs); 
	//delete tree p
	void destroyTree(binarySearchTreeNode<Type> * & p); 
	//insert x into tree p
	void insert(binarySearchTreeNode<Type> * & p, const Type & x); 
	//remove x from tree p
	void remove(binarySearchTreeNode<Type> * & p, const Type & x); 
	//inorder traveral tree p
	void inOrderTraversal(binarySearchTreeNode<Type> * p); 
	//preorder traveral tree p
	void preOrderTraversal(binarySearchTreeNode<Type> * p); 
	//postorder traveral tree p
	void postOrderTraversal(binarySearchTreeNode<Type> * p); 
	//get height of tree p
	int treeHeight(binarySearchTreeNode<Type> * p);	
	//get node count of tree p
	int treeNodeCount(binarySearchTreeNode<Type> * p);
	//get leave count of tree p
	int treeLeaveCount(binarySearchTreeNode<Type> * p);		
	//find min of tree p
	binarySearchTreeNode<Type> * findMin(binarySearchTreeNode<Type> *p) const; 
	//find max of tree p
	binarySearchTreeNode<Type> * findMax(binarySearchTreeNode<Type> *p) const; 
	//find x in tree p
	bool find(const binarySearchTreeNode<Type> *p, const Type & x) const; 
	int larger (int x, int y); 
}; 

//default constructor
template <class Type>
binarySearchTree<Type>::binarySearchTree()
{
	root = NULL; 
}

//copy constructor
template<class Type>
binarySearchTree<Type>::binarySearchTree(const binarySearchTree<Type> & rhs)
{
	if (this != & rhs)
	{
		root = copyTree(root, rhs.root);//actual copy
	}
}
	
//destructor
template<class Type> 
binarySearchTree<Type>::~binarySearchTree()
{
	destroyTree();  
}

//find minimum element in the tree
template<class Type>
binarySearchTreeNode<Type> * binarySearchTree<Type>::findMin() const
{
	return findMin(root); 
}

//find maximum element in the tree
template <class Type>
binarySearchTreeNode<Type> * binarySearchTree<Type>::findMax() const
{
	return findMax(root); 
}
//find x in the tree or not
template<class Type>
bool binarySearchTree<Type>::find (const Type & x) const
{
	return find (root, x); 
}

//check whether the tree is empty or not
template <class Type>
bool binarySearchTree<Type>::isEmpty()
{
	return root == NULL; 
}

//delete the tree
template<class Type>
void binarySearchTree<Type>::destroyTree()
{
	destroyTree(root); 
}

//insert x into the tree
template<class Type>
void binarySearchTree<Type>::insert(const Type & x)
{
	insert(root, x); 
}

//remove x from the tree
template<class Type>
void binarySearchTree<Type>::remove(const Type & x)	
{
	remove(root, x); 
}

//overload operator =
template<class Type>
const binarySearchTree<Type> & binarySearchTree<Type>::operator=(const binarySearchTree<Type> & rhs)
{
	if (this != &rhs)
		copyTree(root, rhs); 

	return this; 
}

//print tree in in-order
template<class Type>
void binarySearchTree<Type>::inOrderTraversal()
{
	inOrderTraversal(root); 
}
//print tree in pre-order
template<class Type>
void binarySearchTree<Type>::preOrderTraversal()
{
	preOrderTraversal(root); 
}
//print tree in post-order
template<class Type>
void binarySearchTree<Type>::postOrderTraversal()
{
	postOrderTraversal(root); 
}

//get the height of the tree
template<class Type>
int binarySearchTree<Type>::treeHeight()
{
	return treeHeight(root); 
}

//get the node count (size) of the tree
template<class Type>
int binarySearchTree<Type>::treeNodeCount()
{
	return treeNodeCount(root); 
}

//get the leave count of the tree
template<class Type>
int binarySearchTree<Type>::treeLeaveCount()
{
	return treeLeaveCount(root);
}


2nd half of Binary Search Header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*****************************************************************
 auxillary functions
 *****************************************************************/

//copy tree rhs to rt
template<class Type>
binarySearchTreeNode<Type> * binarySearchTree<Type>::copyTree(binarySearchTreeNode<Type> * & rt, binarySearchTreeNode<Type> *rhs)
{
	if (rt != NULL)
		destroty(rt); 
	
	if (rhs == NULL)
	{
		rt = NULL;  
	}
	else 
	{
		rt = new binarySearchTreeNode(rhs->info, 
								copyTree(rt->left, rhs->left), 
								copyTree(rt->right, right->right)); 
	}
	
	return rt; 
}

//delete tree p
template<class Type>	
void binarySearchTree<Type>::destroyTree(binarySearchTreeNode<Type> * & p)
{
	binarySearchTreeNode<Type> * tmp; 
	tmp = p; 
	if (p!=NULL)
	{
		destroyTree(p->left); 
		destroyTree(p->right); 
		delete p; 
	}
}
	
//insert x into tree p
template<class Type>	
void binarySearchTree<Type>::insert(binarySearchTreeNode<Type> * & p, const Type & x)
{
	if (p == NULL) 
		p = new binarySearchTreeNode<Type>(x, NULL, NULL); 
	else if (p->element <= x) //ERROR 3 ************************
		insert(p->right, x); 
	else 
		insert(p->left, x); 
}
	
//remove x from tree p
template<class Type>	
void binarySearchTree<Type>::remove(binarySearchTreeNode<Type> * & p, const Type & x)
{
	binarySearchTreeNode<Type> * tmp; 
	//if tree is empty do nothing
	if (p==NULL)
		return;		
	
	if (x < p->element)             //ERROR 4**************************
		remove(p->left, x);		//remove x from left subtree
	else if (x < p->element)//ERROR 5 *****************************
		remove (p->right, x);	//romve x from right subtree
	else						//remove the curent node containing x
	{
		//p have no children or only one child
		if (p->left == NULL || p->right == NULL)
		{
			tmp = p; 
			p = (p->left == NULL)? p->right: p->left; 
			delete tmp; 
		}
		else //p has two children
		{
			p->element = findMin(p->right)->element; 
			remove(p->right, p->element); 
		}
	}
}
	
//inorder traveral tree p
template<class Type>	
void binarySearchTree<Type>::inOrderTraversal(binarySearchTreeNode<Type> * p)
{
	if (p != NULL)
	{
		inOrderTraversal(p->left); 
		cout<< p->element <<endl; 
		inOrderTraversal(p->right); 
	}
}
	
//preorder traveral tree p
template<class Type>	
void binarySearchTree<Type>::preOrderTraversal(binarySearchTreeNode<Type> * p)
{
	if (p != NULL)
	{
		cout<< p->element <<endl; 
		preOrderTraversal(p->left); 
		preOrderTraversal(p->right); 
	}
}
//postorder traveral tree p
template<class Type>	
void binarySearchTree<Type>::postOrderTraversal(binarySearchTreeNode<Type> * p)
{
	if (p != NULL)
	{
		postOrderTraversal(p->left); 
		postOrderTraversal(p->right); 
		cout<< p->element <<endl; 
	}
}
	
//get height of tree p
template<class Type>	
int binarySearchTree<Type>::treeHeight(binarySearchTreeNode<Type> * p)
{
	if (p==NULL)
		return -1; 
	else 
		return 1 + larger(treeHeight(p->left), treeHeight(p->right)); 
}
	
//get node count of tree p
template<class Type>	
int binarySearchTree<Type>::treeNodeCount(binarySearchTreeNode<Type> * p)
{
	if (p==NULL)
		return 0; 
	else 
		return 1 + treeNodeCount(p->left) + treeNodeCount(p->right);   
}
	
//get leave count of tree p
template<class Type>	
int binarySearchTree<Type>::treeLeaveCount(binarySearchTreeNode<Type> * p)
{
	if (p == NULL)
		return 0; 
	else if (p->left == NULL && p->right == NULL)
		return 1; 
	else if (p->left == NULL )
		return treeLeaveCount(p->right); 
	else if (p->right == NULL)
		return treeLeaveCount(p->left); 
	else 
		return treeLeaveCount(p->left) + treeLeaveCount(p->right); 
}
	
//find min of tree p
template<class Type>	
binarySearchTreeNode<Type> * binarySearchTree<Type>::findMin(binarySearchTreeNode<Type> *p) const 
{
	if (p == NULL)
		return NULL; 
	else if (p->left == NULL)
		return p; 
	else 
		return findMin(p->left); 
}
	
//find max of tree p
template<class Type>	
binarySearchTreeNode<Type> * binarySearchTree<Type>::findMax(binarySearchTreeNode<Type> *p) const
{
	if (p == NULL)
		return NULL; 
	else if (p->right == NULL)
		return p; 
	else 
		return findMin(p->right); 

}
	
//find x in tree p
template<class Type>	
bool binarySearchTree<Type>::find(const binarySearchTreeNode<Type> *p, const Type & x) const
{
	if (p == NULL)
		return false;
	else if (p->element == x) //ERROR 1**************************
		return true; 
	else if (x < p->element) //ERROR 2**************************
		return find(p->left, x); 
	else 
		return find (p->right, x); 
}

template<class Type>
int binarySearchTree<Type>::larger(int x, int y)
{
	return (x >= y)? x : y; 
}

#endif 
Last edited on
You'd be amazed how few of us have memorized every single Visual C++ numerical error code. Perhaps you could humour us by posting the actual text of the error too, along with the number of the line that's generating the error?

The people who created the compiler felt that it would be helpful to give you that information. Why did you decide it wouldn't be helpful to give it to us?
Its there in the main post above the code. I also marked the location in the code where the error is. You may have read the post before I edited in the error due to my haste to post I forgot to put that in right away but its corrected now. The error is marked in between 2 comments with * and also marked with a comment itself indicating this is where the error is.
Last edited on
the problem is that you're trying to compare the content of is and cin. That just doesn't work.

Compare the pointers:

if (&is == &cin) // Note: &
You're trying to compare two istream objects, but istream does not provide a comparison operator, which is what the error message is trying to tell you.
http://www.cplusplus.com/reference/istream/istream/

Only way I know of testing if two istream objects are the same is to test to see if they point to the same streambuf.
 
if (is.rdbuf() == cin.rdbuf())



Awesome ok, adding the address operator worked on fixing that error now I am getting 5 errors in the Binary Search Tree header file all dealing with insertion/remove/find( I will mark the error locations in the above code). I am trying to keep the Binary Tree a template (more work more reward), so I do not want to make a brand new function for just this structure type that would defeat of the purpose.

Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const personType' (or there is no acceptable conversion)

Error 2 error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const personType' (or there is no acceptable conversion)

Error 3 error C2679: binary '<=' : no operator found which takes a right-hand operand of type 'const personType' (or there is no acceptable conversion)

Error 4 error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const personType' (or there is no acceptable conversion)

Error 5 error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const personType' (or there is no acceptable conversion)

BTW I really appreciate everything you guys are doing to help and thank you for the explanations as well. Would I need to add some new overload functions to my BinaryTree Class or could I just inherit the overloaded functions from personType ?
Last edited on
Bump ?
I'd say that it's pretty obvious: 'const personType'

Your operator overloadings (line 21 to 25) take a non const reference. Check other operator whether you may add a const or not, too
Thank you guys very much for all your help. These problems were easy to solve, but as you are well aware when the semester is coming to a closing you get a little stressed out the easy problems can become quite the road block and all you need is an extra pair of eyes to assist. There were more errors that came up after the last fix, but I have solved those and now the program is working great.

Once again Thank you.
no problem. That the error occurs on a different location can be quite confusing especially whne dealing with operators and/or templates
Yes they can be thats why I came here lol, but I have learned alot from this and am now applying this newly learnt knowledge to the rest of my programs. Thanks a ton !
Topic archived. No new replies allowed.