c++ - 将值插入以不同方式排序的两个BST中

标签 c++ templates compiler-errors binary-search-tree function-pointers

对于我在学校正在进行的项目,我必须将指向一个对象的指针插入两个BST中。一个BST按APN(唯一键)排序,另一个按价格(非唯一)排序。我们正在使用模板,所以我问教授如何做到这一点,她说要使用函数指针。当我尝试这样做时,遇到了一些我不知道如何解决的错误。

该对象定义为

class House
{
    private:
    string APN; // Unique key
    int price;  // Non-unique key

    string address;
    int bedrooms;
    double bathrooms;
    int sqFt;
}

总的来说,创建对象后,我尝试运行。
uniqueTree->insert(newHouse, comparePrimaryKey);
nonUniqueTree->insert(newHouse, compareSecondaryKey);

其中每个功能定义为
int comparePrimaryKey(const House* &left, const House* &right)
{
    if(left->getAPN() < right->getAPN())
        return -1;
    else
        return 1;
}

int compareSecondaryKey(const House* &left, const House* &right)
{
    if(left->getPrice() < right->getPrice())         // right > left
       return -1;
    else                                            // right < left
       return 1;
}

但我说错了
"Cannot initialize a parameter of type 'int (*)(House *const &, House *const &) 
with an lvalue of type 'int (const House *&, const House *&)'"

在Binary Tree文件中有一个名为rootPtr的BinaryNode对象指针,insert被定义为纯虚函数。
BinaryNode<ItemType>* rootPtr;
virtual bool insert(const ItemType & newData, int compare(const 
ItemType&, const ItemType&)) = 0;

二进制节点类:
template<class T>
class BinaryNode
{   
private:
    T              item;         // Data portion
    BinaryNode<T>* leftPtr;     // Pointer to left child
    BinaryNode<T>* rightPtr;        // Pointer to right child

public:
    // constructors
    BinaryNode(const T & anItem)    {item = anItem; leftPtr = 0; rightPtr = 0;}
    BinaryNode(const T & anItem, 
           BinaryNode<T>* left, 
           BinaryNode<T>* right) {item = anItem; leftPtr = left; rightPtr = right;}
    // mutators
    void setItem(const T & anItem) {item = anItem;}
    void setLeftPtr(BinaryNode<T>* left) {leftPtr = left;}
    void setRightPtr(BinaryNode<T>* right) {rightPtr = right;}
    // accessors
    T getItem() const    {return item;}
    BinaryNode<T>* getLeftPtr() const  {return leftPtr;}
    BinaryNode<T>* getRightPtr() const {return rightPtr;}

    bool isLeaf() const {return (leftPtr == 0 && rightPtr == 0);}
}; 

在BST文件中,插入定义为
template<class ItemType>
bool BinarySearchTree<ItemType>::insert(const ItemType &newEntry, int 
compare(const ItemType &, const ItemType &))
{
    BinaryNode<ItemType>* newNodePtr = new BinaryNode<ItemType>(newEntry);
    BinaryTree<ItemType>::rootPtr = _insert(BinaryTree<ItemType>::rootPtr, 
newNodePtr, compare(newNodePtr->getItem(), BinaryTree<ItemType>::rootPtr()->getItem()));
    return true;
}

我也在BinaryTree::rootPtr行上遇到错误,说
Called object type 'BinaryNode<House *> *' is not a function or function pointer

最佳答案

这是一个解决您的第一个问题的简单程序,我添加了一些函数的定义,以便可以测试您的类和函数。

#include <iostream>
#include <string>


class House
{
    private:
    std::string APN; // Unique key
    int price;  // Non-unique key

    std::string address;
    int bedrooms;
    double bathrooms;
    int sqFt;

public:

    House(std::string apn, int prix)

    {
    APN = apn;
    price = prix;

    }
    std::string getAPN(void)

    {
    return APN;

    }


        int getPrice()
        {

        return price;

        }

};

    int comparePrimaryKey( House *const  &left,  House *const  &right)
{
    if(left->getAPN() < right->getAPN())
        return -1;
    else
        return 1;
}

int compareSecondaryKey( House *const  &left,  House *const  &right)
{
    if(left->getPrice() < right->getPrice())         // right > left
       return -1;
    else                                            // right < left
       return 1;
}

// Main function for the program
int main( )
{

    int PrimaryKey =0;
    int SecondaryKey=0;

    House right("droite",2050);
    House left("gauche",2000);



    PrimaryKey =  comparePrimaryKey(&left, &right);
    SecondaryKey =  compareSecondaryKey(&left, &right);

std::cout<< PrimaryKey << std::endl;
std::cout << SecondaryKey << std::endl;

    std::system("PAUSE");
    return 0;
}

关于c++ - 将值插入以不同方式排序的两个BST中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36042187/

相关文章:

C# 在泛型列表中使用 Equals 方法失败

swift - 静态成员 '<top/center/bottom>' 不能用于 'Alignment' 类型的实例

c++ - 模板、接口(interface)(多重继承)和静态函数(命名构造函数)

c++ - 了解 C++ 编译

java - 不使用局部变量时无法推断Comparator的类型参数

php - 在 PHP 与 C++ 中处理

涉及运算符重载函数的 C++ 链接器错误

c++ - C++ lambda 捕获的引用在 lambda 执行时具有不同的值

c++ - 无法将 const char * 转换为 char *

c++ - 如何知道一个类型是否是 std::vector 的特化?