insert a template class issue
May 11, 2021 at 10:02am UTC
hello developers, I have a problem. I'have a library about RedBackTree.h. It is a template class and I'm creating a series from that class (RedBlackTree<Employee> Employes) then I create a class called Empoyee in my main file and insert it into this array but ı getting this error.
1 2 3 4 5 6 7 8 9 10 11
required from here
RedBlackTree.h:97:23: error: no match for 'operator==' (operand types are 'Employee' and 'Employee' )
if (root->GetValue() == value)
In file included from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iosfwd:40,
from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:38,
from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from GorkemTuneri17244710055.cpp:1:
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/postypes.h:216:5: note: candidate: 'template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)'
operator ==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
Where could I have done wrong? I'm showing header and main file
header
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
using namespace std;
template <class T>
class RBNode
{
private :
bool isRed;
T value;
RBNode<T> *left, *right, *parent;
public :
RBNode(T value)
{
this ->isRed = true ;
this ->value = value;
left = NULL;
right = NULL;
parent = NULL;
}
void Recolor()
{
if (this ->isRed) this ->isRed = false ;
else this ->isRed = true ;
}
bool IsRed()
{
return isRed;
}
T GetValue()
{
return this ->value;
}
RBNode* GetLeft()
{
return this ->left;
}
RBNode* GetRight()
{
return this ->right;
}
RBNode* GetParent()
{
return this ->parent;
}
void ClearParent()
{
this ->parent = NULL;
}
void SetLeft(RBNode* left)
{
this ->left = left;
if (left != NULL) left->parent = this ;
}
void SetRight(RBNode* right)
{
this ->right = right;
if (right != NULL) right->parent = this ;
}
};
template <class T> class RedBlackTree
{
private :
RBNode<T>* root;
RBNode<T>* AccessNode(RBNode<T> *root, T value)
{
if (root->GetValue() == value)
{
return root;
}
else if (root->GetValue() > value)
{
if (root->GetLeft() == NULL)
{
return NULL;
}
else
{
return this ->AccessNode(root->GetLeft(), value);
}
}
else
{
if (root->GetRight() == NULL)
{
return NULL;
}
else
{
return this ->AccessNode(root->GetRight(), value);
}
}
}
void InsertNode(RBNode<T> *root, T value)
{
RBNode<T>* insertedNode = NULL;
if (root->GetValue() == value)
{
//skip
}
else if (root->GetValue() > value)
{
if (root->GetLeft() == NULL)
{
insertedNode = new RBNode<T>(value);
root->SetLeft(insertedNode);
}
else
{
this ->InsertNode(root->GetLeft(), value);
}
}
else
{
if (root->GetRight() == NULL)
{
insertedNode = new RBNode<T>(value);
root->SetRight(insertedNode);
}
else
{
this ->InsertNode(root->GetRight(), value);
}
}
if (insertedNode == NULL) return ;
this ->SolveDoubleRedProblem(root);
insertedNode = NULL;
}
void SolveDoubleRedProblem(RBNode<T> *root)
{
if (root->IsRed() == false ) return ;
if (root == root->GetParent()->GetLeft())
{
if (root->GetParent()->GetRight() == NULL || !root->GetParent()->GetRight()->IsRed()) this ->RightRotate(root);
else
{
root->Recolor();
root->GetParent()->GetRight()->Recolor();
root->GetParent()->Recolor();
if (root->GetParent()->GetParent() == NULL)
{
root->GetParent()->Recolor();
return ;
}
SolveDoubleRedProblem(root->GetParent()->GetParent());
}
}
else
{
if (root->GetParent()->GetLeft() == NULL || !root->GetParent()->GetLeft()->IsRed()) this ->LeftRotate(root);
else
{
root->Recolor();
root->GetParent()->GetLeft()->Recolor();
root->GetParent()->Recolor();
if (root->GetParent()->GetParent() == NULL)
{
root->GetParent()->Recolor();
return ;
}
SolveDoubleRedProblem(root->GetParent()->GetParent());
}
}
}
void LeftRotate(RBNode<T> *root)
{
RBNode<T> *parent = root->GetParent();
if (root->GetLeft() != NULL && root->GetLeft()->IsRed())
{
RBNode<T> *badChild = root->GetLeft();
root->SetLeft(badChild->GetRight());
badChild->SetRight(root);
parent->SetRight(badChild);
root = badChild;
}
root->Recolor();
parent->Recolor();
parent->SetRight(root->GetLeft());
if (parent->GetParent() != NULL)
{
if (parent->GetParent()->GetLeft() == parent) parent->GetParent()->SetLeft(root);
else parent->GetParent()->SetRight(root);
}
else
{
this ->root = root;
this ->root->ClearParent();
root = this ->root;
}
root->SetLeft(parent);
}
void RightRotate(RBNode<T> *root)
{
RBNode<T> *parent = root->GetParent();
if (root->GetRight() != NULL && root->GetRight()->IsRed())
{
RBNode<T> *badChild = root->GetRight();
root->SetRight(badChild->GetLeft());
badChild->SetLeft(root);
parent->SetLeft(badChild);
root = badChild;
}
root->Recolor();
parent->Recolor();
parent->SetLeft(root->GetRight());
if (parent->GetParent() != NULL)
{
if (parent->GetParent()->GetLeft() == parent) parent->GetParent()->SetLeft(root);
else parent->GetParent()->SetRight(root);
}
else
{
this ->root = root;
this ->root->ClearParent();
root = this ->root;
}
root->SetRight(parent);
}
public :
RedBlackTree()
{
this ->root = NULL;
}
bool IsEmpty()
{
return root == NULL;
}
RBNode<T>* AccessNode(T value)
{
if (this ->IsEmpty()) return NULL;
else return this ->AccessNode(root, value);
}
void InsertNode(T value)
{
if (this ->IsEmpty())
{
this ->root = new RBNode<T>(value);
this ->root->Recolor();
}
else this ->InsertNode(this ->root, value);
}
};
Last edited on May 11, 2021 at 7:42pm UTC
May 11, 2021 at 10:36am UTC
The problem is that you compare the values of the RedBlackTree (Line 67/97) but the class Employee does not have an operator==(). So the compiler does not know how to do that.
The operator>() does not exist either.
You need something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
class Employee
{
public :
int IdNumber;
int ID2;
int phoneNumber;
void setIDnubmer(int Name);
void setPhone(int Name);
void setID2(int Name);
Employee();
bool operator ==(const Employee& that) const
{
return (IdNumber == that.IdNumber) && (ID2 == that.ID2) && ...; // Whatever is needed for the comparison
}
bool operator !=(const Employee& that) const
{
return !operator ==(that);
}
};
The other needed operators are similar.
Last edited on May 11, 2021 at 10:58am UTC
May 11, 2021 at 10:42am UTC
As it says in the error message, you need to provide an operator== overload for the Employee class. You also need an operator>.
Something like:
1 2 3 4 5 6 7 8 9 10 11 12 13
class Employee
{
public :
int IdNumber;
int ID2;
int phoneNumber;
void setIDnubmer(int Name);
void setPhone(int Name);
void setID2(int Name);
Employee();
bool operator ==(const Employee& rhs) const { return IdNumber == rhs.IdNumber; }
bool operator >(const Employee& rhs) const { return IdNumber < rhs.IdNumber; }
};
This causes the code to compile with no errors (VS2019 - just warnings).
Last edited on May 11, 2021 at 12:04pm UTC
May 11, 2021 at 3:07pm UTC
thank youu. I have to one question. So, How can I search Employee by IdNumber from Students list using RedBLackTree library?
May 11, 2021 at 4:47pm UTC
If the code in the first post is from a library, it is outdated and not good code for modern C++.
To find a node with a particular value, then you use AccessNode() which either returns nullptr if not found or a pointer to the node with the required value.
So simply (not tried):
1 2 3 4
if (const auto ndptr (students.AccessNode(e)}; ndptr != nullptr )
std::cout << "found " << ndptr->value.Idnumber << " " << ndptr->value.Id2 << '\n' ;
else
std::cout << "Not found\n" ;
May 11, 2021 at 7:42pm UTC
you saved my life thank you
Topic archived. No new replies allowed.