【问题标题】:How to derive abstract template classes, with template-types as function parameters (C++11)如何派生抽象模板类,模板类型作为函数参数 (C++11)
【发布时间】:2019-12-07 20:47:54
【问题描述】:

我被分配编写一个派生自抽象模板类“binaryTreeType”的类“binaryExpressionTree”。 binaryExpressionTree 是字符串类型。作为作业的一部分,我必须从 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;

    binaryTreeType();
    //Default constructor
};

binaryTreeType<elemType>::binaryTreeType()
{
}

#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";
  }

这里是 main.cpp:

#include <iostream>
#include <iomanip>
#include <fstream>
#include "binaryExpressionTree.h"

int main() 
{
binaryExpressionTree mainTree = binaryExpressionTree(); //Error:[cquery] allocating an object of abstract class type 'binaryExpressionTree'
return 0;
}

问题是,由于 binaryExpressionTree 是 String 类型的派生类,它不知道“elemType”是什么意思,我需要更改 searchItem, insertItem and deleteItem 到字符串和对象。但是一旦我这样做了,编译器就不再识别出我正在覆盖虚函数(因为我已经更改了它们的参数),并将 binaryExpressionTree 声明为一个抽象类。我该如何解决这个问题,以便我可以覆盖函数并使 binaryExpressionTree 非抽象?

【问题讨论】:

  • 嗨。请提供一个最小的可重现示例。也就是说,我可以在不添加任何额外内容的情况下编译。
  • 好点。我刚刚添加在

标签: c++11 templates abstract-class virtual-functions template-classes


【解决方案1】:

假设抽象类是这样定义的:

template <typename elemType>
class binaryTreeType { ... }

你应该如下定义你的类:

class binaryExpressionTree : public binaryTreeType<String> { ... }

编辑:原始问题已编辑。

您错误地声明了覆盖函数(在 binaryExpressionTree 内)。 你的声明是这样的:

bool search(const string& searchItem) const = 0;

这样的声明创建了一个纯虚方法(因为在声明末尾有= 0。纯虚方法(又名抽象方法)是一种必须被派生类覆盖的方法。因此,binaryTreeTypebinaryExpressionTree 中声明它的方法是纯虚拟的,以便 实现。

具有尚未实现的抽象方法的类无法实例化 - 这是您的编译器正在生成的错误。

相反,您应该像这样声明您的方法:

virtual bool search(const elemType& searchItem) const;

这样的声明会创建常规的虚函数,它会覆盖父实现(在这种情况下不存在)。

TL;DR - 删除 = 0

【讨论】:

  • 我做了,虽然它是 ,因为我使用的是库中的#include
  • 我需要两个类的更详细的代码 sn-p 来理解问题。
  • 抱歉信息不足,我对这些东西还很陌生。我刚刚用足够的代码更新了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-01
相关资源
最近更新 更多