My problem is, when i call int (*comp)(...) it seg faults on me. I haven't used pointers to functions all that much, and ecspecially not when the target function is a class member - even more so, a template.
Since you're gonna look at my code anyway and if you have extra time:
1. Most importantly, how do i fix the seg fault?
2. Does this look like i'm on the right track?
3. How do i initialize the first 3 variables in my code? (i want to make the private, static, const members)
4. Do you know where i can get some data to test this with? (perhaps a flat file with a couple thousand records)
btw, i know i'll need some destructors
Code:#ifndef BTREE_H #define BTREE_H //work around - need to fix this (seems to be soemthing about it being a template) const short comp_less = -1; const short comp_geater = +1; const short comp_equal = 0; template<typename _T = int> class BTree { typedef int key_type; public: BTree(void); BTree(const BTree &, const void * = NULL); BTree(const _T &, const void * = NULL); key_type insert(const _T &); private: _T data; key_type key; BTree * right; BTree * left; int (*comp)(const _T &, const _T &); static inline int compare(const _T &, const _T &); static int __defaultCompare(const _T &, const _T &); }; template<typename _T> BTree<_T>::BTree(const _T &srcNode, const void *compareFunction) { if(!compareFunction) comp = BTree<_T>::__defaultCompare; data = srcNode; right = left = NULL; } template<typename _T> BTree<_T>::key_type BTree<_T>::insert(const _T &srcNode) { BTree *side = NULL; //SEGFAULT ON CALL TO COMP // FROM DECLARATION int (*comp)(const _T &, const _T &); // FROM CONSTRUCTOR: comp = __defaultCompare; // FROM DECLARATION static int __defaultCompare(const _T &, const _T &); if(comp(srcNode, data) == comp_less) side = left; else side = right; //following commented out to find seg fault /*if(!side) side = new BTree(srcNode, comp); else side->insert(srcNode);*/ return 0; } template<typename _T> int BTree<_T>::__defaultCompare(const _T &a, const _T &b) { if(a > b) return +1; else if(a < b) return -1; else return 0; return 0; } #endif



LinkBack URL
About LinkBacks


