【发布时间】:2010-04-22 11:14:48
【问题描述】:
我在使用模板和继承将代码分解为可重用部分时遇到了一些问题。我想实现我的树类和 avltree 类使用相同的节点类,并且 avltree 类从树类继承一些方法并添加一些特定的方法。所以我想出了下面的代码。编译器在 tree.h 中抛出一个错误,如下所示,我真的不知道如何克服这个问题。任何帮助表示赞赏! :)
node.h:
#ifndef NODE_H
#define NODE_H
#include "tree.h"
template <class T>
class node
{
T data;
...
node()
...
friend class tree<T>;
};
#endif
树.h
#ifndef DREVO_H
#define DREVO_H
#include "node.h"
template <class T>
class tree
{
public: //signatures
tree();
...
void insert(const T&);
private:
node<T> *root; //missing type specifier - int assumed. Note: C++ does not support default-int
};
//implementations
#endif
avl.h
#ifndef AVL_H
#define AVL_H
#include "tree.h"
#include "node.h"
template <class T>
class avl: public tree<T>
{
public: //specific
int findMin() const;
...
protected:
void rotateLeft(node<T> *)const;
private:
node<T> *root;
};
#endif
avl.cpp(我尝试将头文件从实现中分离出来,在我开始将 avl 代码与树代码结合之前它就起作用了)
#include "drevo"
#include "avl.h"
#include "vozlisce.h"
template class avl<int>; //I know that only avl with int can be used like this, but currently this is doesn't matter :)
//implementations
...
【问题讨论】:
标签: c++ inheritance templates data-structures refactoring