i created a stuct which contains one character and one string
struct M2E
{ char english;
string morse;}
by using code given by, i created a binary tree of M2E which is bintree
but i want to sort these M2Es in string morse order(“*” less than “-“)
so i did a operater overloading in struct M2E
bool operator == (M2E& other) const
{
return morse.compare(other.morse);
}
but i keep giving the following error message when im compling
no match for "operator ==" in ((binNode<M2E>*)this)->binNode<M2E>::nodeData == dataItem
note:candidates are :bool M2E::operator==(M2E&) const
the code i am using for binary tree which is bintree.h is:
template <typename dataType> class bintree
{
private:
binNode<dataType> *root;
int numItems;
void insert(const dataType& newData)
{
// insert the newData into the tree
if (root == NULL)
{
root = new binNode<dataType>(newData);
}
else
{
root->insert(root, newData);
}
numItems++;
}
the code im using for binary node which is binnode.h is:
template <typename dataType> class binNode
{
private:
// private data ====================================
dataType nodeData;
binNode<dataType> *left, *right;
void insert(binNode<dataType>* &root, const dataType& dataItem)
{
if (nodeData == dataItem)
{
throw std::invalid_argument("dataItem already in tree");
}
if (dataItem < nodeData)
{
if (left == NULL)
{
left = new binNode(dataItem);
}
else
{
left->insert(left, dataItem);
}
}
else
{
if (right == NULL)
{
right = new binNode(dataItem);
}
else
{
right->insert(right, dataItem);
}
}
rebalance(root);
}
thx for helping
The problem is that you are taking a
const datatype dataItemininsert(), but theoperator==takes a non-constM2Eparameter.You need to specify the
operator==parameter type to beconst:Remember that
constis a contract: the function promises not to change the value of its parameter. Soinsert()makes this promise, butoperator==does not, soinsert()cannot make that operator call it as-is. By adding theconsttooperator==‘s parameter type, you makeoperator==make the same promise, soinsert()can call it