我被指定编写一个类“ binaryExpressionTree”,该类派生自抽象模板类“ binaryTreeType”。 binaryExpressionTree的类型为String。作为分配的一部分,我必须从binaryTreeType覆盖这3个虚函数:
//Header File Binary Search Tree
#ifndef H_binaryTree
#define H_binaryTree
#include <iostream>
using namespace std;
//Definition of the Node
template <class elemType>
struct nodeType
{
elemType info;
nodeType<elemType> *lLink;
nodeType<elemType> *rLink;
};
//Definition of the class
template <class elemType>
class binaryTreeType
{
public:
virtual bool search(const elemType& searchItem) const = 0;
virtual void insert(const elemType& insertItem) = 0;
virtual void deleteNode(const elemType& deleteItem) = 0;
};
#endif
这是到目前为止我对binaryExpressionTree的了解:
#define EXPRESSIONTREE_H
#include "binaryTree.h"
#include <iostream>
#include <string>
class binaryExpressionTree : public binaryTreeType<string> {
public:
void buildExpressionTree(string buildExpression);
double evaluateExpressionTree();
bool search(const string& searchItem) const = 0;
void insert(const string& insertItem) = 0;
void deleteNode(const string& deleteItem) = 0;
};
这里是binaryExpressionTree.cpp:
#include <string>
#include <cstring>
#include <stack>
#include <cstdlib>
#include <cctype>
#include "binaryExpressionTree.h"
#include "binaryTree.h"
using namespace std;
bool binaryExpressionTree::search(const string& searchItem) const {
return false;
}
void binaryExpressionTree::insert(const string& insertItem) {
cout << "this";
}
void binaryExpressionTree::deleteNode(const string& deleteItem) {
cout << "this";
}
问题是,因为binaryExpressionTree是String类型的派生类,所以它不知道“ elemType”是什么意思,我需要更改searchItem, insertItem and deleteItem
字符串和对象。但是,一旦这样做,编译器就不再意识到我正在覆盖虚拟函数(因为我已经更改了它们的参数),并且将binaryExpressionTree声明为抽象类。如何解决此问题,以便可以覆盖这些函数并使非BinaryExpressionTree抽象化?
假设抽象类是这样定义的:
template <typename elemType>
class binaryTreeType { ... }
您应该按照以下方式定义您的课程:
class binaryExpressionTree : public binaryTreeType<String> { ... }