简体   繁体   English

C2678:插入二进制搜索树C ++时出错

[英]C2678: error on insertion of Binary search tree c++

Using visual studios 2015 for C++ implementation I am receiving a compilation error in my insertHelper() . 使用Visual insertHelper() 2015 for C ++实现,我在insertHelper()收到编译错误。

Insert Helper is a recursive function listed below. 插入帮助器是下面列出的递归函数。

    template < typename DataType, typename KeyType >
    void BSTree<DataType, KeyType>::insertHelper(BSTreeNode *&p,
    const DataType &newDataItem)

    // Recursive helper function for insert. Inserts newDataItem in
    // the subtree pointed to by p.

{
    if (p == NULL) {
        p->dataItem = newDataItem;
        p->left = NULL;
        p->right = NULL;
    }
    else {
        if (p->dataItem < newDataItem) { // <- error triggers here
            insertHelper(p->left, newDataItem);
        }
        else {
            insertHelper(p->right, newDataItem);
        }
    }
}

Insert Helper is called by insert here: Insert Helper在这里通过insert调用:

 template < typename DataType, typename KeyType >
    void BSTree<DataType, KeyType>::insert(const DataType &newDataItem)

// Inserts newDataItem into a tree. If an data item with the same key
// as newDataItem already exists in the tree, then updates that
// data item's data with newDataItem's data.

{
    insertHelper(root, newDataItem);
}

And a simplified version of my trees header file is here. 我的树头文件的简化版本在这里。

#ifndef BSTREE_H
#define BSTREE_H
#include <stdexcept>
#include <iostream>
using namespace std;

template < typename DataType, class KeyType >    // DataType : tree data item
class BSTree                                     // KeyType : key field
{
public:

    // Constructor
    BSTree();                         // Default constructor
    // Binary search tree manipulation operations
    void insert(const DataType& newDataItem);  // Insert data item
protected:
    class BSTreeNode                  // Inner class: facilitator for the BSTree class
    {
    public:

        // Constructor
        BSTreeNode(const DataType &nodeDataItem, BSTreeNode *leftPtr, BSTreeNode *rightPtr);

        // Data members
        DataType dataItem;         // Binary search tree data item
        BSTreeNode *left,    // Pointer to the left child
            *right;   // Pointer to the right child
    };

    // Recursive helpers for the public member functions -- insert
    // prototypes of these functions here.
    void insertHelper(BSTreeNode *&p, const DataType &newDataItem);

    // Data member
    BSTreeNode *root;   // Pointer to the root node
};

#endif  // define BSTREE_H

Test Data and initialization : 测试数据和初始化:

class TestData
{
  public:

    void setKey ( int newKey )
        { keyField = newKey; }   // Set the key

    int getKey () const
        { return keyField; }     // Returns the key

  private:

    int keyField;                // Key for the data item
};

int main()
{
    BSTree<TestData,int> testTree;   // Test binary search tree
    TestData testData;               // Binary search tree data item
}

I don't understand why I can't use if (p->dataItem < newDataItem){} . 我不明白为什么我不能使用if (p->dataItem < newDataItem){} I've tried if (p.dataItem < newDataItem){} and even just if (dataItem < newDataItem){} . 我已经尝试过if (p.dataItem < newDataItem){} ,甚至只是if (dataItem < newDataItem){} And I am getting nowhere fast with this bug. 而且我没有很快解决这个错误。 Any help would be appreciated. 任何帮助,将不胜感激。

The error reads: 该错误显示为:

C2678 : binary '<': no operator found which takes a left-hand BST operand of type 'TestData' (or there is no acceptable conversion). C2678:二进制'<':未找到采用“ TestData”类型的左侧BST操作数的运算符(或没有可接受的转换)。

if (p->dataItem < newDataItem) { // <- error triggers here

base on 基于

BSTree<TestData,int> testTree;

we know that p->dataItem and newDataItem are of type TestData . 我们知道p->dataItemnewDataItem的类型为TestData

So the comparison above expands to 所以上面的比较扩展到

if (p->dataItem.operator<(newDataItem))

Now lets look at the error message again: 现在,让我们再次查看错误消息:

binary '<': no operator found which takes a left-hand BST operand of type 'TestData' (or there is no acceptable conversion).

It was looking for a binary '<' operator. 它正在寻找二进制“ <”运算符。 You haven't provided one, so the language can't implement your requested < comparison. 您尚未提供一个,因此该语言无法实现您请求的<比较。

class TestData
{
  public:

    void setKey ( int newKey )
        { keyField = newKey; }   // Set the key

    int getKey () const
        { return keyField; }     // Returns the key

    bool operator < (const TestData& rhs) const
        { return keyField < rhs.keyField; }

  private:

    int keyField;                // Key for the data item
};

or 要么

class TestData
{
  public:

    void setKey ( int newKey )
        { keyField = newKey; }   // Set the key

    int getKey () const
        { return keyField; }     // Returns the key

    friend bool operator < (const TestData&, const TestData&);

  private:

    int keyField;                // Key for the data item
};

bool operator < (const TestData& lhs, const TestData& rhs)
{
    return lhs.keyField < rhs.keyField;
}

Live demo: http://ideone.com/Y7JGCD 现场演示: http//ideone.com/Y7JGCD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM