- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写这个程序作为家庭作业,在我添加 createList 函数之前它没有抛出 C2259 错误。我不知道这个问题是从哪里来的。我是 C++ 的新手,所以这可能很容易解决,但我完全迷路了。
这是我的主要功能:
#include <iostream>
#include "binarySearchTree.h"
#include "orderedLinkedList.h"
using namespace std;
int main()
{
bSearchTreeType<int> treeRoot; //error C2259: 'bSearchTreeType<int>'
//cannot instantiate abstract class
orderedLinkedList<int> newList;
int num;
cout << "Enter numbers ending with -999" << endl;
cin >> num;
while (num != -999)
{
treeRoot.insert(num);
cin >> num;
}
cout << endl << "Tree nodes in inorder: ";
treeRoot.inorderTraversal();
cout << endl;
cout << "Tree Height: " << treeRoot.treeHeight()
<< endl;
treeRoot.createList(newList);
cout << "newList: ";
newList.print();
cout << endl;
system("pause");
return 0;
}
这是 binarySearchTree.h:
//Header File Binary Search Tree
#ifndef H_binarySearchTree
#define H_binarySearchTree
#include <iostream>
#include"binaryTree.h"
using namespace std;
template<class elemType>
class bSearchTreeType : public binaryTreeType < elemType >
{
public:
//function to determine if searchItem is in search tree
bool search(const elemType& searchItem) const;
void insert(const elemType& insertItem);
void deleteNode(const elemType& deleteItem);
//update: void createList(const elemType& createItem);
virtual void createList(const elemType& newList) = 0;
private:
void deleteFromTree(nodeType<elemType> *&p);
};
template<class elemType>
void bSearchTreeType<elemType>::createList(const elemType& createItem)
{
nodeType<elemType> *current;
nodeType<elemType> *trailCurrent;
nodeType<elemType> *newNode;
newNode = new nodeType < elemType > ;
newNode->info = createItem;
newNode->lLink = nullptr;
newNode->rLink = nullptr;
if (root == nullptr)
{
root = newNode;
}
}
template<class elemType>
bool bSearchTreeType<elemType>::search(const elemType& searchItem)const
{
nodeType<elemType> *current;
bool found = false;
if (root == NULL)
{
cout << "Cannot search an empty tree." << endl;
}
else
{
current = root;
while (current != NULL && !found)
{
if (current->info == searchItem)
{
found = true;
}
else if (current->info > searchItem)
{
current = current->lLink;
}
else
{
current = current->rLink;
}
}
}
return found;
}
template<class elemType>
void bSearchTreeType<elemType>::insert(const elemType& insertItem)
{
nodeType<elemType> *current;
nodeType<elemType> *trailCurrent;
nodeType<elemType> *newNode;
newNode = new nodeType < elemType > ;
newNode->info = insertItem;
newNode->lLink = NULL;
newNode->rLink = NULL;
if (root == NULL)
{
root = newNode;
}
else
{
current = root;
while (current != NULL)
{
trailCurrent = current;
if (current->info == insertItem)
{
cout << "The item to be inserted is already in the tree";
cout << "Duplicates are not allowed." << endl;
return;
}
else if (current->info > insertItem)
{
current = current->lLink;
}
else
{
current = current->rLink;
}
}
if (trailCurrent->info > insertItem)
{
trailCurrent->lLink = newNode;
}
else
{
trailCurrent->rLink = newNode;
}
}
}
template<class elemType>
void bSearchTreeType<elemType>::deleteNode(const elemType& deleteItem)
{
nodeType<elemType> *current;
nodeType<elemType> *trailCurrent;
bool found = false;
if (root == NULL)
{
cout << "Cannot delete from an empty tree." << endl;
}
else
{
current = root;
trailCurrent = root;
while (current != NULL && !found)
{
if (current->info == deleteItem)
{
found = true;
}
else
{
trailCurrent = current;
if (current->info > deleteItem)
{
current = current->lLink;
}
else
{
current = current->rLink;
}
}
}
if (current == NULL)
{
cout << "The item to be deleted is not in the tree." << endl;
}
else if (found)
{
if (current == root)
{
deleteFromTree(root);
}
else if (trailCurrent->info > deleteItem)
{
deleteFromTree(trailCurrent->lLink);
}
else
{
deleteFromTree(trailCurrent->rLink);
}
}
else
{
cout << "The item to be deleted is not in the tree." << endl;
}
}
}
template<class elemType>
void bSearchTreeType<elemType>::deleteFromTree(nodeType<elemType>* &p)
{
nodeType<elemType> *current;
nodeType<elemType> *trailCurrent;
nodeType<elemType> *temp;
if (p == NULL)
{
cout << "Error: The node to be deleted is NULL." << endl;
}
else if (p->lLink == NULL && p->rLink == NULL)
{
temp = p;
p = NULL;
delete temp;
}
else if (p->lLink == NULL)
{
temp = p;
p = temp->rLink;
delete temp;
}
else if (p->rLink == NULL)
{
temp = p;
p = temp->lLink;
delete temp;
}
else
{
current = p->lLink;
trailCurrent = NULL;
while (current->rLink != NULL)
{
trailCurrent = current;
current = current->rLink;
}
p->info = current->info;
if (trailCurrent == NULL)
{
p->lLink = current->lLink;
}
else
{
trailCurrent->rLink = current->lLink;
}
delete current;
}
}
#endif
另外,我有 112% 的把握确定我的 createList 函数是错误的,不会创建随机数树,所以我也需要一些帮助。
更新:binaryTree.h 定义
#ifndef H_binaryTree
#define H_binaryTree
#include<iostream>
using namespace std;
//Define the node
template<class elemType>
struct nodeType
{
elemType info;
nodeType<elemType> *lLink;
nodeType<elemType> *rLink;
};
template<class elemType>
class binaryTreeType
{
public:
const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&);
bool isEmpty() const;
void inorderTraversal() const;
void preorderTraversal() const;
void postorderTraversal() const;
int treeHeight() const;
int treeNodeCount() const;
int treeLeavesCount() const;
void destroyTree();
virtual bool search(const elemType& searchItem) const = 0;
virtual void insert(const elemType& insertItem) = 0;
virtual void deleteNode(const elemType& deleteItem) = 0;
virtual void createList(const elemType& createItem) = 0;
binaryTreeType(const binaryTreeType<elemType>& otherTree);
binaryTreeType();
~binaryTreeType();
protected:
nodeType<elemType> *root;
private:
void copyTree(nodeType<elemType>*& copiedTreeRoot, nodeType<elemType>* otherTreeRoot);
void destroy(nodeType<elemType>* &p);
void inorder(nodeType<elemType> *p)const;
void preorder(nodeType<elemType> *p)const;
void postorder(nodeType<elemType> *p) const;
int height(nodeType<elemType> *p)const;
int max(int x, int y) const;
int nodeCount(nodeType<elemType> *p)const;
int leavesCount(nodeType<elemType> *p)const;
};
template <class elemType>
binaryTreeType<elemType>::binaryTreeType()
{
root = NULL;
}
template <class elemType>
bool binaryTreeType<elemType>::isEmpty() const
{
return (root == NULL);
}
template <class elemType>
void binaryTreeType<elemType>::inorderTraversal() const
{
inorder(root);
}
template <class elemType>
void binaryTreeType<elemType>::preorderTraversal() const
{
preorder(root);
}
template <class elemType>
void binaryTreeType<elemType>::postorderTraversal() const
{
postorder(root);
}
template <class elemType>
int binaryTreeType<elemType>::treeHeight() const
{
return height(root);
}
template <class elemType>
int binaryTreeType<elemType>::treeNodeCount() const
{
return nodeCount(root);
}
template <class elemType>
int binaryTreeType<elemType>::treeLeavesCount() const
{
return leavesCount(root);
}
template <class elemType>
void binaryTreeType<elemType>::copyTree(nodeType<elemType>* &copiedTreeRoot,
nodeType<elemType>* otherTreeRoot)
{
if (otherTreeRoot == NULL)
copiedTreeRoot = NULL;
else
{
copiedTreeRoot = new nodeType<elemType>;
copiedTreeRoot->info = otherTreeRoot->info;
copyTree(copiedTreeRoot->lLink, otherTreeRoot->lLink);
copyTree(copiedTreeRoot->rLink, otherTreeRoot->rLink);
}
}
template <class elemType>
void binaryTreeType<elemType>::inorder(nodeType<elemType> *p) const
{
if (p != NULL)
{
inorder(p->lLink);
cout << p->info << " ";
inorder(p->rLink);
}
}
template <class elemType>
void binaryTreeType<elemType>::preorder(nodeType<elemType> *p) const
{
if (p != NULL)
{
cout << p->info << " ";
preorder(p->lLink);
preorder(p->rLink);
}
}
template <class elemType>
void binaryTreeType<elemType>::postorder(nodeType<elemType> *p) const
{
if (p != NULL)
{
postorder(p->lLink);
postorder(p->rLink);
cout << p->info << " ";
}
}
//Overload the assignment operator
template <class elemType>
const binaryTreeType<elemType>& binaryTreeType<elemType>::operator=(const binaryTreeType<elemType>& otherTree)
{
if (this != &otherTree) //avoid self-copy
{
if (root != NULL) //if the binary tree is not empty,
//destroy the binary tree
destroy(root);
if (otherTree.root == NULL) //otherTree is empty
root = NULL;
else
copyTree(root, otherTree.root);
}//end else
return *this;
}
template <class elemType>
void binaryTreeType<elemType>::destroy(nodeType<elemType>* &p)
{
if (p != NULL)
{
destroy(p->lLink);
destroy(p->rLink);
delete p;
p = NULL;
}
}
template <class elemType>
void binaryTreeType<elemType>::destroyTree()
{
destroy(root);
}
//copy constructor
template <class elemType>
binaryTreeType<elemType>::binaryTreeType
(const binaryTreeType<elemType>& otherTree)
{
if (otherTree.root == NULL) //otherTree is empty
root = NULL;
else
copyTree(root, otherTree.root);
}
//Destructor
template <class elemType>
binaryTreeType<elemType>::~binaryTreeType()
{
destroy(root);
}
template<class elemType>
int binaryTreeType<elemType>::height
(nodeType<elemType> *p) const
{
if (p == NULL)
return 0;
else
return 1 + max(height(p->lLink), height(p->rLink));
}
template <class elemType>
int binaryTreeType<elemType>::max(int x, int y) const
{
if (x >= y)
return x;
else
return y;
}
template <class elemType>
int binaryTreeType<elemType>::nodeCount(nodeType<elemType> *p) const
{
return nodeCount(root);
}
template <class elemType>
int binaryTreeType<elemType>::leavesCount(nodeType<elemType> *p) const
{
return leavesCount(root);
}
#endif
最佳答案
virtual void createList(const elemType& newList) = 0 ;
/// ~~~~ shouldn't be used in derived class
纯虚函数应该只出现在你的抽象类中,我假设它是 binaryTreeType
关于c++ - 帮助解决错误 C2259 : cannot instantiate abstract class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36831131/
我刚遇到 MSVC(版本 12 更新 5)的问题: 如果模板函数具有通过 SFINAE 禁用的重载,则显式实例化模板函数会失败。但是,调用该函数(从而隐式实例化它)是有效的。 示例代码: #inclu
我正在阅读一本关于 DI 的书,该书总是谈论 DI 框架“实例化对象图”。为什么这样说而不是“实例化对象”? 最佳答案 对象图由保存彼此引用的对象组成。在这种情况下,图的另一个名称是网络。 如果 IO
一个类Customers实例化许多其他类(例如CustomersFromMysql、CustomersFromPostgeSQL),所有查询数据库都返回客户名称。现在,这些客户名称返回为例如 name
当我尝试调用 listenEventReducer 时,出现了这个奇怪的错误。该方法知道类型,但仍然不确定我哪里出错了。 import 'package:test/test.dart'; enum O
我正在尝试使用 org.hibernate.Interceptor.instantiate() 来拦截实例化(显然)并使用默认构造函数之外的构造函数手动实例化特定对象。如果要阅读此方法的 JavaDo
public class TestingClass { public static void main(String[] args) { int numberRooms = 6
为什么 C++ 以这样的方式创建,如果您有一个类 A 并声明一个类型 A 的数组,那么整个数组将填充使用该类的默认构造函数实例化的对象? 最佳答案 因为当您创建一个给定大小的数组时,数组的每个元素都必
考虑下面的例子 template struct S { A a; void foo() {} }; template void bar() { S *p = 0; } templat
Note that code is instantiated only for member functions that are called. For class templates, membe
当我尝试运行这段代码时: import java.io.*; import java.util.*; public class TwoColor { public static void ma
每当我尝试在 Unity 3D 中实例化粒子系统时,命令都会定位粒子但不会播放/运行动画。 这是我的代码 GameObject impactGO = Instantiate(impactEffect,
我使用以下代码在 verilog 中实例化二维内存 reg [15:0] data_pattern_even [3:0] = {16'hFFFF,16'hFFFF,16'hFFFF,16'hFFFF
假设我获得了我作为 String 创建的类的名称。 .如何使用该字符串中包含的名称实例化类?我知道它将派生自某个父类,但实际类会有所不同。 最佳答案 var instance : MyClass =
python 的 attrs 包提供了一种在实例化时验证传递的变量的简单方法 (example taken from attrs page): >>> @attr.s ... class C(obje
我收到 java 空指针异常。我无法解决它。我已在 testbase 类中初始化驱动程序,并希望在我的 Testing_TVO 类中使用相同的驱动程序 这是我的测试基类 public class te
我对 Java 编程还比较陌生,可能错过了一些明显的东西,所以请耐心等待。 我正在创建一个程序,该程序使用 Swing API 和 JDesktopPane 来创建 GUI。主屏幕上有一个按钮,上面写
python 的 attrs 包提供了一种在实例化时验证传递的变量的简单方法 (example taken from attrs page): >>> @attr.s ... class C(obje
C++ 模板中的“延迟实例化”是什么意思? 最佳答案 延迟实例化是指直到第一次使用对应的实体时才实例化模板。例如,您有一个模板化函数: template void YourFunction() {
当我阅读 spring 教程时,我发现了这样的内容: LocalChangeInterceptor localChangeInterceptor; localChangeInterceptor = n
我正在实现 unforgettable factory .一切正常,但有一件事:类(class)有时没有注册。 我认为关键部分是 Registrar::registered成员。如果使用它,“真正有趣
我是一名优秀的程序员,十分优秀!