2012-05-21 112 views
0

我创建了使用由给定的代码包含一个字符和一个字符串C++二叉树超载

struct M2E 
{ char english; 
string morse;} 

一个stuct,我创建M2E的二进制树是二叉树 ,但我想在字符串这些M2Es排序为了莫尔斯(“*”小于“ - ”) 所以我做了算子在结构M2E超载

bool operator == (M2E& other) const 
{ 
    return morse.compare(other.morse); 
} 

,但我继续给下面的错误消息时,即时通讯compling

no match for "operator ==" in ((binNode<M2E>*)this)->binNode<M2E>::nodeData == dataItem 
note:candidates are :bool M2E::operator==(M2E&) const 

我使用二进制树的代码是bintree.h是:

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++; 
    } 

使用二进制节点的编码IM是binnode.h是:

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的帮助

+0

实际上我把另一个重载运算符>除非你使用Visual Studio这是不工作的,以及 –

回答

1

问题是您在insert()中采用const datatype dataItem,但operator==采用非常数M2E p arameter。

你需要指定operator==参数类型为const

bool operator == (const M2E& other) const { 
    //... 
} 

记住const是一个合同:该功能保证不会修改其参数的值。所以insert()作出了这个承诺,但operator==没有,所以insert()不能使该运算符按原样调用它。通过添加constoperator==的参数类型,你让operator==做出了同样的承诺,所以insert()可以称之为

+0

,上这将编译好。 –

+0

@ std''OrgnlDave - 哪个版本?它认为新标准在符合标准 – Attila

+0

10和11中相当不错。这是令人费解的,但BC编译它很好,并在运行时愉快地递归。我相信它也未能展示正确的行为(错误和拒绝编译)在更微不足道的情况下,但这是很方便的。 http://ideone.com/STAXJ –