0

現在、バイナリ検索ツリーを設定しており、テンプレートを使用して、バイナリ検索ツリー内のデータの種類を簡単に変更できます。現在、ツリーに格納されるデータを含むstudentRecordクラスのオーバーロードに問題があります。BSTがコンテンツの1つ(この場合は学生ID)に基づいて2つのオブジェクトを適切に比較できるように、このクラス内の比較演算子をオーバーロードする必要があります。ただし、studentRecord内の演算子がオーバーロードされているにもかかわらず、適切な比較はまだ行われていません。

以下の詳細:

現在、タイプのbstオブジェクトstudentTreeが作成されています

bst<studentRecord *> studentTree;

studentRecordは次のクラスです。

// studentRecord class
class studentRecord{
public:
    // standard constructors and destructors
    studentRecord(int studentID, string lastName, string firstName, string academicYear){ // constructor
        this->studentID=studentID;
        this->lastName=lastName;
        this->firstName=firstName;
        this->academicYear=academicYear;
    }

    friend bool operator > (studentRecord &record1, studentRecord &record2){
        if (record1.studentID > record2.studentID)
            cout << "Greater!" << endl;
        else
            cout << "Less then!" << endl;
        return (record1.studentID > record2.studentID);
    }

private:
    // student information
    string studentID;
    string lastName;
    string firstName;
    string academicYear;
};

新しいアイテムが私のBSTに追加されるときはいつでも、それらを互いに比較する必要があります。したがって、studentRecordクラスをオーバーロードして、この比較プロセスが発生したときに、studentIDが比較されるようにしました(そうでない場合は、無効な比較が行われます)。

ただし、挿入関数がオーバーロードされた比較関数を使用することはありません。代わりに、2つのオブジェクトを別の方法で比較しているようで、BST内で無効な並べ替えが発生します。私の挿入関数の一部を以下に示します。テンプレート処理が行われるため、toInsertとnodePtr->dataの両方がstudentRecord型である必要があることに注意してください。

// insert (private recursive function)
template<typename bstType>
void bst<bstType>::insert(bstType & toInsert, bstNodePtr & nodePtr){
    // check to see if the nodePtr is null, if it is, we've found our insertion point (base case)
    if (nodePtr == NULL){
        nodePtr = new bst<bstType>::bstNode(toInsert);
    }

    // else, we are going to need to keep searching (recursive case)
    // we perform this operation recursively, to allow for rotations (if AVL tree support is enabled)
    // check for left
    else if (toInsert < (nodePtr->data)){ // go to the left (item is smaller)
        // perform recursive insert
        insert(toInsert,nodePtr->left);

        // AVL tree sorting
        if(getNodeHeight(nodePtr->left) - getNodeHeight(nodePtr->right) == 2 && AVLEnabled)
            if (toInsert < nodePtr->left->data)
                rotateWithLeftChild(nodePtr);
            else
                doubleRotateWithLeftChild(nodePtr);
    }

また、ここにBSTクラス定義の一部があります

// BST class w/ templates
template <typename bstType>
class bst{

private: // private data members

    // BST node structure (inline class)
    class bstNode{
    public: // public components in bstNode

        // data members
        bstType data;
        bstNode* left;
        bstNode* right;

        // balancing information
        int height;

        // constructor
        bstNode(bstType item){
            left = NULL;
            right = NULL;
            data = item;
            height = 0;
        }

        // destructor
        // no special destructor is required for bstNode     
    };

    // BST node pointer
    typedef bstNode* bstNodePtr;

public: // public functions.....

これを引き起こしている可能性があるものについてのアイデアはありますか?間違ったクラスまたは間違った関数をオーバーロードしていますか?どんな助けでもありがたいです-非常に多くの異なることが一度に起こっているので、私は道に迷っているようです。

4

2 に答える 2

2

次のようにテンプレートクラスをインスタンス化します。

bst<studentRecord *> studentTree;

したがって、bstType == studentRecord *

その場合、挿入は次のようになります。

template<studentRecord*>
void bst<studentRecord*>::insert(studentRecord*& toInsert, bst<studentRecord*>::bstNodePtr & nodePtr);

したがって、ポインタ比較を行っているので、Ashaがすでに指摘しているように、演算子が呼び出されないのはこのためです。

さらに、より大きい演算子(>)のみをオーバーロードしますが、挿入ではより小さい演算子(<)を使用します。挿入時にstudentRecord型の2つのオブジェクトを実際に比較する場合、コードはコンパイルすらされないはずであり、適切な未満の演算子が見つからないと文句を言う必要があります。

さらに、コード内のいくつかの問題を指摘できます。

  1. studentRecord.studentIDは文字列型ですか?ただし、コンストラクターで整数を割り当てようとします。これは単に整数をcharに変換し、文字を文字列に割り当てるだけなので、おそらく意図したものではありません。
  2. より小さい演算子がありません。

以下のコードと、studentRecord型の2つのインスタンスを比較するときに演算子が呼び出されることを示すいくつかのコードを示します。また、studentRecordクラスの演算子定義にコメントを付けることで(->コンパイルエラー)、不足している未満の演算子の影響を確認できます。

class studentRecord
{
public:

    studentRecord(int studentID) : studentID(studentID)
    { 
    }

    bool operator > (studentRecord &record)
    {
        return (studentID > record.studentID);
    }

    /* Uncomment to get rid of the compile error!
    bool operator < (studentRecord &record)
    {
        return studentID < record.studentID;
    }
    */

private:
    // student information
    int studentID;
};

int main()
{
    studentRecord r1(10);
    studentRecord r2(5);

    if ( r1 < r2 )
    {
        cout << "It works! " << "r1 less than r2" << endl;
    }
    else
    {
        cout << "It works! " << "r1 greater than r2" << endl;
    }

    if ( r1 > r2 )
    {
        cout << "It works! " << "r1 greater than r2" << endl;
    }
    else
    {
        cout << "It works! " << "r1 less than r2" << endl;
    }
}

最後のコメントとして、他の比較演算子(> =、<=、==、!=)も提供することをお勧めします。

于 2011-02-23T09:29:17.050 に答える
1

あなたのツリーはポインタのツリーです。したがって、要素をツリーに挿入しようとすると、ポインターの値が比較されます。したがって、オーバーロードされた演算子は呼び出されません。オーバーロードされた演算子を使用する場合は、次のように作成する必要がありますbst<studentrecord>

于 2011-02-23T06:14:14.827 に答える