E
Ethan
//eylds.h
#ifndef _EYL_DS_H_
#define _EYL_DS_H_
#include <cstdlib>
#include <algorithm>
#include "crtp.h"
using eylcrtp::Comparable;
enum direction_t {LEFT=0, RIGHT=1};
template <class KeyType, class DataType> class AvlNode;
template <class KeyType, class DataType> class AvlTree;
typedef AvlNode<class KeyType, class DataType> NodeType;
template <class KeyType, class DataType>
class AvlNode : public Comparable< AvlNode<KeyType, DataType> > {
public:
enum {MAX_SUB = 2};
AvlNode(KeyType k, DataType d) : _key(k), _data(d) { reset(); }
/* ************************************** problem here
**************************************
~AvlNode() {
if (_subNodes
#ifndef _EYL_DS_H_
#define _EYL_DS_H_
#include <cstdlib>
#include <algorithm>
#include "crtp.h"
using eylcrtp::Comparable;
enum direction_t {LEFT=0, RIGHT=1};
template <class KeyType, class DataType> class AvlNode;
template <class KeyType, class DataType> class AvlTree;
typedef AvlNode<class KeyType, class DataType> NodeType;
template <class KeyType, class DataType>
class AvlNode : public Comparable< AvlNode<KeyType, DataType> > {
public:
enum {MAX_SUB = 2};
AvlNode(KeyType k, DataType d) : _key(k), _data(d) { reset(); }
/* ************************************** problem here
**************************************
~AvlNode() {
if (_subNodes
) delete _subNodes
;
if (_subNodes
if (_subNodes
) delete _subNodes
;
}
**************************************************************************************************
*/
bool operator<(const AvlNode& a) const { return _key < a._key; }
KeyType key() { return _key; }
DataType data() { return _data;}
NodeType* child(direction_t d) { return _subNodes[d]; }
short bal() { return _bal; }
void reset() { _bal = 0; _subNodes
}
**************************************************************************************************
*/
bool operator<(const AvlNode& a) const { return _key < a._key; }
KeyType key() { return _key; }
DataType data() { return _data;}
NodeType* child(direction_t d) { return _subNodes[d]; }
short bal() { return _bal; }
void reset() { _bal = 0; _subNodes
= NULL; _subNodes
=
NULL;}
friend class AvlTree<KeyType, DataType>;
private:
KeyType _key;
DataType _data;
short _bal;
NodeType* _subNodes[MAX_SUB];
};
template <class KeyType, class DataType>
class AvlTree {
public:
AvlTree() : _root(NULL) {}
~AvlTree() { if (_root) delete _root; }
private:
NodeType* _root;
};
#endif
//main.cc
#include <iostream>
#include "eylds.h"
using namespace std;
int main() {
int a = 5;
int *p1 = &a;
int b = 6;
int *p2 = &b;
AvlNode<int, int*> n1(a, p1);
AvlNode<int, int*> n2(b, p2);
cout << (n1 == n2) << endl;
return 0;
}
these code compiled and run ok.
But if I added the destructor, (the one commented out above), it
wouldn't compile
eylds.h: In instantiation of ‘AvlNode<KeyType, DataType>’:
eylds.h:22: instantiated from ‘AvlNode<KeyType, DataType>::~AvlNode
() [with KeyType = int, DataType = int*]’
test_eylds.cc:11: instantiated from here
eylds.h:38: error: ‘AvlNode<KeyType, DataType>::_key’ has incomplete
type
eylds.h:13: error: forward declaration of ‘struct KeyType’
eylds.h:39: error: ‘AvlNode<KeyType, DataType>::_data’ has incomplete
type
eylds.h:13: error: forward declaration of ‘struct DataType’
I couldn't figure out why, thank you in advance for your help!
NULL;}
friend class AvlTree<KeyType, DataType>;
private:
KeyType _key;
DataType _data;
short _bal;
NodeType* _subNodes[MAX_SUB];
};
template <class KeyType, class DataType>
class AvlTree {
public:
AvlTree() : _root(NULL) {}
~AvlTree() { if (_root) delete _root; }
private:
NodeType* _root;
};
#endif
//main.cc
#include <iostream>
#include "eylds.h"
using namespace std;
int main() {
int a = 5;
int *p1 = &a;
int b = 6;
int *p2 = &b;
AvlNode<int, int*> n1(a, p1);
AvlNode<int, int*> n2(b, p2);
cout << (n1 == n2) << endl;
return 0;
}
these code compiled and run ok.
But if I added the destructor, (the one commented out above), it
wouldn't compile
eylds.h: In instantiation of ‘AvlNode<KeyType, DataType>’:
eylds.h:22: instantiated from ‘AvlNode<KeyType, DataType>::~AvlNode
() [with KeyType = int, DataType = int*]’
test_eylds.cc:11: instantiated from here
eylds.h:38: error: ‘AvlNode<KeyType, DataType>::_key’ has incomplete
type
eylds.h:13: error: forward declaration of ‘struct KeyType’
eylds.h:39: error: ‘AvlNode<KeyType, DataType>::_data’ has incomplete
type
eylds.h:13: error: forward declaration of ‘struct DataType’
I couldn't figure out why, thank you in advance for your help!