将值插入两个不同的BST中

时间:2016-03-16 16:57:49

标签: 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文件中,insert定义为

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

1 个答案:

答案 0 :(得分:0)

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

#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;
}
相关问题