Operatör bir BST içinde Şablonlarıyla Aşırı yükleme

oy
0

Şu anda beni kolayca ikili arama ağacının içindeki verilerin türünü değiştirmek için izin şablonlar kullanılarak, bir ikili arama ağacı kurulum var. Şu anda sorun ağacında saklanmasına verileri içeren studentRecord sınıfını aşırı yaşıyorum. Benim BST düzgün içerikleri birine dayalı iki nesne karşılaştırmak böylece ben (bu durumda, öğrenci kimliği), bu sınıfın içinde karşılaştırma operatörlerini aşırı gerekir. Ancak, studentRecord içinde operatörleri aşırı rağmen, doğru karşılaştırmalar hala gerçekleşmiyor.

Detaylar aşağıda:

Şu anda, BST nesne studentTree türünden bağımsız olarak, yaratılmıştır

bst<studentRecord *> studentTree;

studentRecord şu sınıftır:

// 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;
};

yeni öğeler benim BST eklenir zaman, birbirleriyle karşılaştırılmalıdır. Bu nedenle, bu karşılaştırma işlemi gerçekleştiğinde, studentIDs karşılaştırıldığında, böylece studentRecord sınıfı aşırı istediğini (aksi takdirde, geçersiz bir karşılaştırma yapılmaktadır).

Ancak, benim ekleme fonksiyonu asla benim aşırı karşılaştırma işlevlerini kullanır. Bunun yerine, BST içinde geçersiz sıralamaya sonuçlanan iki nesneleri başka bir yol karşılaştırarak gibi görünüyor. Benim insert fonksiyonunun Bölüm altındadır - toInsert ve nodePtr-> veriler hem sebebiyle şablon süreç husul, tip studentRecord olması gerektiğini not etmek önemlidir.

// 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);
    }

Ayrıca, burada BST sınıfı Definition bir kısmıdır

// 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.....

Bu neyin sebep olabileceğini herhangi bir fikir? Yanlış sınıf veya yanlış işlev aşırı muyum? Herhangi bir yardım takdir - Bu kadar çok farklı şeyler aynı anda gerçekleştiğini beri kaybolmadan gibi görünüyor.

Oluştur 23/02/2011 saat 07:09
kaynak kullanıcı
Diğer dillerde...                            


2 cevaplar

oy
1

Ağacın bir ağaçtır işaretçiler . Yani değerleri ağaca bir öğe eklemek çalıştığınızda işaretçiler karşılaştırılır. Yani aşırı operatör çağrılmaz. Eğer aşırı operatörü kullanmak istiyorsanız o zaman oluşturmalıbst<studentrecord>

Cevap 23/02/2011 saat 07:14
kaynak kullanıcı

oy
2

Bu gibi şablon sınıf örneğini:

bst<studentRecord *> studentTree;

Yani bstType == studentRecord *

insert bu daha sonra şöyle görünür:

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

böylece bir işaretçi karşılaştırma yapıyoruz ve Asha allready işaret olarak operatör çağrılmaz bu yüzden.

Daha böylece operatör daha sadece büyük aşırı (>) ancak ekin sen operatör daha az kullanmak (<). Gerçekten ekin türü studentRecord iki nesne karşılaştırmak istiyorsanız kod bile derlemek should't ve operatör daha az uygun bir bulamadığını şikayetçi olmalıdır.

Daha yüzden Kodunuzdaki çeşitli sorunlarla işaret edebilir:

  1. studentRecord.studentID dize türünde mi? Ancak bunu yapıcı bir tamsayı atamak deneyin. Eğer amaçlanan bu yüzden büyük ihtimalle değil ne - Bu sadece ateşinde ve dize karakter atamak tamsayı dönüştürür.
  2. Sen operatör daha az kayıp.

tipi studentRecord iki örneğini karşılaştırırken size kodu ve operatörü gösteren bazı thest kodu altında çağrılır. (-> derlemek hata) Ayrıca studentRecord sınıfında operatör tanımı yorumlayarak operatör daha az kayıp etkilerinin ne olacağını görebilirsiniz.

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;
    }
}

Bir bitiş yorum olarak muhtemelen> =, <=, == ve (çok başka karşılaştırma operatörlerini sağlamak için iyi bir fikir olurdu! =.

Cevap 23/02/2011 saat 10:29
kaynak kullanıcı

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more