ऑपरेटर एक BST के अंदर टेम्पलेट्स के साथ ओवरलोडिंग

वोट
0

मैं वर्तमान में उपयोग टेम्पलेट्स मुझे आसानी से द्विआधारी खोज वृक्ष के भीतर डेटा के प्रकार को बदलने के लिए अनुमति देने के लिए एक द्विआधारी खोज वृक्ष सेटअप है,। फिलहाल, मैं मुसीबत studentRecord वर्ग जो डेटा पेड़ में संग्रहीत करने के लिए होता है अधिक भार हो रही है। मैं तो यह है कि मेरी BST ठीक से उनकी सामग्री में से एक के आधार पर दो वस्तुओं की तुलना कर सकते हैं (इस मामले में, छात्र आईडी) इस वर्ग के भीतर तुलना ऑपरेटरों को ओवरलोड की जरूरत है। हालांकि, 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 वर्ग ओवरलोड इतनी है कि जब इस तुलना प्रक्रिया होती है, तो studentIDs तुलना की जाती है चाहता था (जिसे अन्यथा, गलत तुलना बनाया जाएगा)।

लेकिन, मेरा प्रविष्टि समारोह कभी नहीं मेरी अतिभारित तुलना कार्यों का उपयोग करता। इसके बजाय, यह दो वस्तुओं किसी अन्य तरीके की तुलना करने के लिए, BST भीतर अवैध रूप से क्रमबद्ध जिसका परिणाम लगता है। मेरी डालने समारोह का एक हिस्सा नीचे है - यह ध्यान रखें कि दोनों toInsert और nodePtr-> डेटा templating प्रक्रिया घटित होने के कारण, प्रकार 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 वर्ग defintion के एक हिस्से को है

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

यह क्या कारण हो सकता है पर कोई भी विचार? मैं गलत वर्ग या गलत समारोह ओवरलोडिंग हूँ? किसी भी मदद की सराहना की है - मैं इतने सारे अलग अलग बातें एक ही बार में हो रही हैं के बाद से खो रहा है कर रहे हैं।

23/02/2011 को 07:09
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


2 जवाब

वोट
1

आपका पेड़ के एक पेड़ है संकेत दिए गए । तो जब आप पेड़ के मूल्यों में एक तत्व सम्मिलित करने के लिए कोशिश संकेत तुलना में है। तो अपने अतिभारित ऑपरेटर नहीं बुलाया जाता है। आप अतिभारित ऑपरेटर का उपयोग करना चाहते हैं तो आप बनाना चाहिएbst<studentrecord>

23/02/2011 को 07:14
का स्रोत उपयोगकर्ता

वोट
2

आप इस तरह अपने टेम्पलेट वर्ग का दृष्टांत:

bst<studentRecord *> studentTree;

तो bstType == studentRecord *

डालने इस तो दिखाई देता है:

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

तो आप एक सूचक तुलना कर रहे हैं और इस वजह से आपके ऑपरेटर के रूप में आशा allready कहा नहीं कहा जाता है।

अधिक ताकि आप ऑपरेटर से केवल अधिक से अधिक भार (>) लेकिन डालने में आप ऑपरेटर से भी कम समय का उपयोग (<)। क्या तुम सच में डालने में टाइप studentRecord की दो वस्तुओं की तुलना करेंगे, तो कोड भी संकलन should't और इसकी शिकायत करनी चाहिए कि यह उपयुक्त ऑपरेटर से भी कम समय खोजने में असमर्थ है।

अधिक तो मैं अपने कोड में कई समस्या के कर सकते हैं:

  1. studentRecord.studentID प्रकार स्ट्रिंग की है? लेकिन अगर आप यह निर्माता में एक पूर्णांक आवंटित करने के लिए प्रयास करें। यह केवल पूर्णांक चार और स्ट्रिंग के लिए चरित्र आवंटित करने के लिए परिवर्तित कर देंगे - आप क्या इतना काफी शायद नहीं करना है।
  2. आप ऑपरेटर से भी कम समय याद कर रहे हैं।

नीचे आप कोड और कुछ thest कोड है कि ऑपरेटर को दर्शाता है कहा जाता हो जाता है जब प्रकार studentRecord के दो उदाहरणों की तुलना। आप यह भी देख सकते हैं कि 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;
    }
}

एक अंत टिप्पणी के रूप में शायद यह भी अन्य तुलना ऑपरेटरों प्रदान करने के लिए (> =, <=, == और एक अच्छा विचार होगा! =।

23/02/2011 को 10:29
का स्रोत उपयोगकर्ता

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