简体   繁体   English

c ++模板和继承

[英]c++ templates and inheritance

I'm experiencing some problems with breaking my code to reusable parts using templates and inheritance. 我在使用模板和继承将代码分解为可重用部分时遇到一些问题。 I'd like to achieve that my tree class and avltree class use the same node class and that avltree class inherits some methods from the tree class and adds some specific ones. 我想实现我的树类和avltree类使用相同的节点类,并且avltree类从树类继承一些方法并添加一些特定的方法。 So I came up with the code below. 所以我想出了下面的代码。 Compiler throws an error in tree.h as marked below and I don't really know how to overcome this. 编译器在tree.h中抛出错误,如下所示,我真的不知道该如何克服。 Any help appreciated! 任何帮助表示赞赏! :) :)

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

tree.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 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 (I tried separating headers from implementation, it worked before I started to combine avl code with tree code) 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
...

Both tree.h and node.h try to include each other, the include guards will prevent one of them from seeing the other. tree.hnode.h尝试互相包含,include防护将阻止其中一个看到对方。

Instead of #include "tree.h" try forward declaring tree like: 代替#include "tree.h"尝试像下面这样声明树:

template <class T>
class tree;

in node.h node.h

EDIT: As sbi suggested in a comment, it makes more sense to forward declare tree in node.h than the other way around, since it's about granting tree access to node through a friend declaration. 编辑:正如sbi在评论中建议的那样,在node.h转发声明tree比在其他方法中更有意义,因为这是关于通过friend声明授予对node tree访问权限。

Don't #include "tree.h" in "node.h". 不要在“ node.h”中#include“ tree.h”。

Also, you've declared root in both the tree and avl classes. 另外,您已经在treeavl类中声明了root Qualify tree::root as protected and remove avl::root . tree::root限定为protected并删除avl::root

Your problem is that tree.h includes node.h and vice versa. 您的问题是tree.h包含node.h,反之亦然。 I would not have thought it is necessary (or makes much sense) for the node to have to know about the tree or to grant it friendship, so I'd remove that. 我不会认为节点必须了解树或授予它友谊是必要的(或很有意义),因此我将其删除。

The problem is because of the circular dependency of header files between tree.h and node.h . 问题是由于tree.h和node.h之间头文件的循环依赖关系。 Since node.h includes tree.h, while compiling the tree class, compiler doesn't know what is the type of node. 由于node.h包含tree.h,因此在编译树类时,编译器不知道节点的类型是什么。 Since you are using it just for declaring a friend there is no need to include the header file tree.h in node.h 由于您仅将其用于声明朋友,因此无需在node.h中包含头文件tree.h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM