c++ - 继承问题 - '{' token 之前的预期类名

标签 c++ inheritance compiler-errors

所以我收到这个错误,它与我的 AVLTree 类继承我的 BTree 类的方式有关。据我所知,编译器表现得好像找不到 BTree.h(在同一目录中),或者只是不喜欢我包含它的方式。

我一直在网上四处寻找,但我看到的关于此问题的大多数示例都涉及有人忘记包含某些内容,所以我不确定下一步该怎么做。

编辑:我还添加了 BTree.h 的代码示例

如有任何帮助,我们将不胜感激。

g++ -g -w -Wall TreeTest.cpp BTree.h BinaryTree.h AVLTree.h -o testTrees In file included from TreeTest.cpp:12:0: AVLTree.h:22:29: error: expected class-name before '{' token TreeTest.cpp: In function

代码如下:

#ifndef AVLTree_H
#define AVLTree_H

#include <vector>
#include <iostream>
#include "BTree.h"

using std::vector;
using std::cout;
using std::string;

template <class T>
class AVLTree : public BTree{
    public:
        struct TreeNode{
            TreeNode * leftChild, 
                 * rightChild;
            T key;
            vector<T> data;
            int size;
            int height;
            bool deleted;
        };

        //Standard tree functions
        AVLTree();
        virtual ~AVLTree();
        virtual bool isEmpty();
        virtual int getSize();
        virtual int getHeight();
        virtual int insert(T key, T data); //returns number of insert calls
        virtual int remove(T key);         //returns number of remove calls
        virtual int contains(T key);       //removes number of contains calls, or 0 if doesn't exist
        virtual std::vector<T> getData(T key);

        //Special functions
        virtual void displayAll();
        virtual double getAverageDepth(); 
        virtual int getTotalIPL();  //Retrieves internal path length from root

    private: 
        int size;
        TreeNode * root;

        int * contains(T key, TreeNode * node, int calls);
        int insert(T key, T data, TreeNode *& node, int calls);
        int remove(T key, TreeNode *& node, int calls);
        int getDepth(TreeNode*curr, int total);
        int getIPL(TreeNode * start, int level);
        void rotateLeft(TreeNode *& node);
        void doubleLeft(TreeNode *& node);
        void rotateRight(TreeNode *& node);
        void doubleRight(TreeNode *& node);
        int max(int a, int b);
        int getHeight( TreeNode * t );
        void display(TreeNode * node, string indent, bool last);
        TreeNode * makeNode(T key, T data);
        TreeNode * getNode(T key, TreeNode * node);
        void destroySubTree(TreeNode * start);
};

这里是 BTree.h:

#ifndef bTree_H
#define bTree_H

#include <vector>

template <class T>
class BTree{
    public:
        struct TreeNode{
            TreeNode * leftChild, 
                 * rightChild;
            T key;
            std::vector<T> data;
            int size;
        };

        //Standard tree functions
        virtual ~BTree();
        virtual bool isEmpty() = 0;
        virtual int getSize() = 0;
        virtual int getHeight() = 0;
        virtual int insert(T key, T data) = 0; //returns number of insert calls
        virtual int remove(T key) = 0;         //returns number of remove calls
        virtual int contains(T key) = 0;        
        virtual std::vector<T> getData(T key) = 0;

        //Special functions
        virtual void displayAll() = 0;
        virtual double getAverageDepth() = 0; 
        virtual int getTotalIPL() = 0;  //Retrieves internal path length from root

    private: 
        int size;
        TreeNode * root;

};

template <class T> 
BTree<T>::~BTree<T>(){}

#endif

最佳答案

由于 BTree 是一个类模板,您不能直接继承它,而必须指定它的实例化。也就是说,您必须提供模板参数,在您的情况下,它应该与实例化 AVLTree 的类型相同。

template <class T>
class AVLTree:public BTree<T>

关于c++ - 继承问题 - '{' token 之前的预期类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19469478/

相关文章:

c - 标量初始值设定项 c 中的多余元素

c++ - 在模板中使用 std::max 时出错

java - 异常类不工作

c++ - 如何返回指向数组中对象的指针?

c++ - 是否可以使用调试器调试 UnhandledExceptionFilters?

c++ - 将两个链表中的特定值相加时出现问题

c++ - 基础对象和继承对象的 vector

c++ - 不断向 glBufferData 添加新点的最佳方法是什么?

swift 继承 : Super's Super

c++ - 从外部定义的类继承时无效使用不完整类型类错误