简体   繁体   中英

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() .

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:

 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){} . I've tried if (p.dataItem < newDataItem){} and even just 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).

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

base on

BSTree<TestData,int> testTree;

we know that p->dataItem and newDataItem are of type 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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