मैं वर्तमान में उपयोग टेम्पलेट्स मुझे आसानी से द्विआधारी खोज वृक्ष के भीतर डेटा के प्रकार को बदलने के लिए अनुमति देने के लिए एक द्विआधारी खोज वृक्ष सेटअप है,। फिलहाल, मैं मुसीबत 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.....
यह क्या कारण हो सकता है पर कोई भी विचार? मैं गलत वर्ग या गलत समारोह ओवरलोडिंग हूँ? किसी भी मदद की सराहना की है - मैं इतने सारे अलग अलग बातें एक ही बार में हो रही हैं के बाद से खो रहा है कर रहे हैं।













