如何正确使用模板类的嵌套类
How to correctly use a nested class of a template class?
all!我正在尝试实现一个简单的模板类二进制搜索树。我遇到了几个关于函数定义的问题。以下是我的BST类代码
#ifndef BINARY_SEARCH_TREE_H
#define BINARY_SEARCH_TREE_H
#include <iostream>
#include <stdlib.h>
using namespace std;
template<class T>
class BST
{
struct TreeNode {
TreeNode* left;
TreeNode* right;
T value;
};
//member functions
void destroyTree(TreeNode* leaf);
void insert(T key, TreeNode* leaf);
TreeNode* search(T key, TreeNode* leaf);
void printInOrder(TreeNode* leaf);
void printPreOrder(TreeNode* leaf);
void printPostOrder(TreeNode* leaf);
//memebr variables
TreeNode* root;
public:
enum Traversal { INORDER, PREORDER, POSTORDER };
BST();
BST(T key);
~BST();
void insert(T key);
TreeNode* search(T key);
void printTree(T option);
void destroyTree();
};
template <class T>
BST<T>::BST()
{
root = NULL;
};
template <class T>
BST<T>::BST(T key)
{
root = new TreeNode;
root->left = NULL;
root->right = NULL;
root->value = key;
};
template <class T>
BST<T>::~BST()
{
destroyTree();
};
template <class T>
void BST<T>::destroyTree(TreeNode* leaf)
{
if (leaf->left != NULL) destroyTree(leaf->left);
if (leaf->right != NULL) destroyTree(leaf->right);
delete leaf;
leaf = nullptr;
};
template <class T>
void insert(T key, BST<T>::TreeNode* leaf)
{
if (leaf->value == key)
{
cout << "failed inserting node: duplicate item" << endl;
return;
}
else if (leaf->value < key)
{
if (leaf->right != NULL) insert(key, leaf->right);
else
{
TreeNode newNode = new TreeNode;
newNode->left = NULL;
newNode->right = NULL;
newNode->value = key;
leaf->right = newNode;
}
}
else
{
if (leaf->left != NULL) insert(key, leaf->left);
else
{
TreeNode newNode = new TreeNode;
newNode->left = NULL;
newNode->right = NULL;
newNode->value = key;
leaf->left = newNode;
}
}
};
template <class T>
BST<T>::TreeNode* BST<T>::search(T key, TreeNode* leaf)
{
if (leaf == NULL) return NULL;
if (leaf->value == key) return leaf;
else if (leaf->vluae < key) return search(key, leaf->right);
else return search(key, leaf->left);
};
template <class T>
void printInOrder(TreeNode* leaf)
{
if (leaf->left != NULL) printInOrder(leaf->left);
cout << leaf->value << " ";
if (leaf->right != NULL) printInOrder(leaf->right);
};
template <class T>
void printPreOrder(TreeNode* leaf)
{
cout << leaf->value << " ";
if (leaf->left != NULL) printPreOrder(leaf->left);
if (leaf->right != NULL) printPreOrder(leaf->right);
};
template <class T>
void printPostOrder(TreeNode* leaf)
{
if (leaf->left != NULL) printPostOrder(leaf->left);
if (leaf->right != NULL) printPostOrder(leaf->right);
cout << leaf->value << " ";
};
template <class T>
void BST<T>::insert(int key)
{
if (this->root == NULL)
{
this->root = new TreeNode;
this->root->left = NULL;
this->root->right = NULL;
this->root->value = key;
}
else insert(key, root);
};
template <class T>
BST<T>::TreeNode* BST<T>::search(int key)
{
search(key, this->root);
};
template <class T>
void BST<T>::printTree(int option)
{
switch (option)
{
case BST<T>::INORDER:
printInOrder(this->root);
cout << endl;
break;
case BST<T>::POSTORDER:
printPostOrder(this->root);
cout << endl;
break;
case BST<T>::PREORDER:
printPreOrder(this->root);
cout << endl;
break;
}
};
template <class T>
void BST<T>::destroyTree()
{
destroyTree(this->root);
};
#endif
正如您所看到的,对于void insert(T key, BST<T>::TreeNode* leaf)
和BST<T>::TreeNode* BST<T>::search(T key, TreeNode* leaf)
函数,我需要使用TreeNode
类执行一些操作,比如返回它的对象或将它传递给函数,函数是BST
类中定义的嵌套类型。我遇到的错误是语法错误,但我不知道我在代码中哪里做错了。任何建议或建议都将不胜感激!
您应该:
-
用
BST<T>::TreeNode
替换每一个TreeNode
,因为可能有不同的TreeNode
定义,所以编译器需要知道您正在谈论的定义。 -
在每个
BST<T>::TreeNode
前面加上typename
。BST<T>::TreeNode
可能有几种不同的定义,甚至有些定义不是类型,所以您需要告诉编译器它是一种类型。
#include <iostream>
#include <stdlib.h>
using namespace std;
template<class T>
class BST
{
struct TreeNode {
TreeNode* left;
TreeNode* right;
T value;
};
//member functions
void destroyTree(TreeNode* leaf);
void insert(T key, TreeNode* leaf);
TreeNode* search(T key, TreeNode* leaf);
void printInOrder(TreeNode* leaf);
void printPreOrder(TreeNode* leaf);
void printPostOrder(TreeNode* leaf);
//memebr variables
TreeNode* root;
public:
enum Traversal { INORDER, PREORDER, POSTORDER };
BST();
BST(T key);
~BST();
void insert(T key);
TreeNode* search(T key);
void printTree(T option);
void destroyTree();
};
template <class T>
BST<T>::BST()
{
root = NULL;
};
template <class T>
BST<T>::BST(T key)
{
root = new TreeNode;
root->left = NULL;
root->right = NULL;
root->value = key;
};
template <class T>
BST<T>::~BST()
{
destroyTree();
};
template <class T>
void BST<T>::destroyTree(TreeNode* leaf)
{
if (leaf->left != NULL) destroyTree(leaf->left);
if (leaf->right != NULL) destroyTree(leaf->right);
delete leaf;
leaf = nullptr;
};
template <class T>
void BST<T>::insert(T key, typename BST<T>::TreeNode* leaf)
{
if (leaf->value == key)
{
cout << "failed inserting node: duplicate item" << endl;
return;
}
else if (leaf->value < key)
{
if (leaf->right != NULL) insert(key, leaf->right);
else
{
BST<T>::TreeNode* newNode = new TreeNode;
newNode->left = NULL;
newNode->right = NULL;
newNode->value = key;
leaf->right = newNode;
}
}
else
{
if (leaf->left != NULL) insert(key, leaf->left);
else
{
BST<T>::TreeNode* newNode = new TreeNode;
newNode->left = NULL;
newNode->right = NULL;
newNode->value = key;
leaf->left = newNode;
}
}
};
template <class T>
typename BST<T>::TreeNode* BST<T>::search(T key, typename BST<T>::TreeNode* leaf)
{
if (leaf == NULL) return NULL;
if (leaf->value == key) return leaf;
else if (leaf->vluae < key) return search(key, leaf->right);
else return search(key, leaf->left);
};
template <class T>
void printInOrder(typename BST<T>::TreeNode* leaf)
{
if (leaf->left != NULL) printInOrder(leaf->left);
cout << leaf->value << " ";
if (leaf->right != NULL) printInOrder(leaf->right);
};
template <class T>
void printPreOrder(typename BST<T>::TreeNode* leaf)
{
cout << leaf->value << " ";
if (leaf->left != NULL) printPreOrder(leaf->left);
if (leaf->right != NULL) printPreOrder(leaf->right);
};
template <class T>
void printPostOrder(typename BST<T>::TreeNode* leaf)
{
if (leaf->left != NULL) printPostOrder(leaf->left);
if (leaf->right != NULL) printPostOrder(leaf->right);
cout << leaf->value << " ";
};
template <class T>
void BST<T>::insert(T key)
{
if (this->root == NULL)
{
this->root = new TreeNode;
this->root->left = NULL;
this->root->right = NULL;
this->root->value = key;
}
else insert(key, root);
};
template <class T>
typename BST<T>::TreeNode* BST<T>::search(T key)
{
search(key, this->root);
};
template <class T>
void BST<T>::printTree(T option)
{
switch (option)
{
case BST<T>::INORDER:
printInOrder(this->root);
cout << endl;
break;
case BST<T>::POSTORDER:
printPostOrder(this->root);
cout << endl;
break;
case BST<T>::PREORDER:
printPreOrder(this->root);
cout << endl;
break;
}
};
template <class T>
void BST<T>::destroyTree()
{
destroyTree(this->root);
};
现在应该工作了,但充满了小错误,比如:CCD_ 11,template <class T> void insert
而不是template <class T> void BST<T>::insert
相关文章:
- 如何在嵌套类中正确使用友元声明?
- 单独定义模板化嵌套类方法的正确语法
- Clang拒绝类模板的嵌套类仅通过专门化定义的代码是正确的吗
- 如何正确使用hpp文件和cpp文件中的嵌套类
- 用于在一维数组上嵌套循环操作的正确 openmp 指令
- 在嵌套名称空间范围中使用名称空间的正确方法
- C++:将 Args && ... _args传递到嵌套列表的正确方法?
- C++ 嵌套循环输出不正确
- 嵌套类C 的正确模板
- 从外部类C++正确调用嵌套类中的函数
- 如何正确声明模板类的嵌套类的友元
- 如何正确使用模板类的嵌套类
- 正确使用嵌套环和鳍
- 嵌套循环语句结果不正确
- 在类嵌套静态常量成员变量初始化 Clang vs GCC 哪个编译器是正确的
- 嵌套循环的时间复杂度:cn(n+1)/2从何而来
- 如何正确声明嵌套类
- 如何在C++的嵌套循环中正确更新进度条
- 我需要使用嵌套的 while 循环打印一个正方形。我想我已经用正确的逻辑对其进行了编码,但它没有给我所需的输出
- 我是否正确地将嵌套的 lua 表作为 C 函数的参数