C2678:插入二叉搜索树 C++ 时出错
C2678: error on insertion of Binary search tree c++
使用Visual Studios 2015进行C++实现,我在insertHelper()
中收到编译错误。
插入帮助程序是下面列出的递归函数。
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);
}
}
}
插入帮助程序通过在此处插入调用:
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);
}
我的树头文件的简化版本在这里。
#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
测试数据和初始化:
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
}
我不明白为什么我不能使用if (p->dataItem < newDataItem){}
.我已经尝试过if (p.dataItem < newDataItem){}
,甚至只是if (dataItem < newDataItem){}
.而且我对这个错误无处可去。任何帮助将不胜感激。
错误如下:
C2678:二进制"<":未找到采用类型为"TestData"的左侧 BST 操作数的运算符(或者没有可接受的转换(。
if (p->dataItem < newDataItem) { // <- error triggers here
基于以下
BSTree<TestData,int> testTree;
我们知道p->dataItem
和newDataItem
属于 TestData
型 .
所以上面的比较扩展到
if (p->dataItem.operator<(newDataItem))
现在让我们再次查看错误消息:
binary '<': no operator found which takes a left-hand BST operand of type 'TestData' (or there is no acceptable conversion).
它正在寻找一个二进制"<"运算符。您尚未提供,因此该语言无法实现您请求的<
比较。
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
};
或
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;
}
现场演示:http://ideone.com/Y7JGCD
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 为x86而非x64编译时出错
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 为重写std::exception的库生成swig接口时出错
- 导入/导出变量时出错
- PrintWindow函数在notepad.exe中出错