我是 C++ 新手,我很困惑为什么我不能将 Node* 作为 BST 类中函数的返回类型(我必须将其用作返回类型)。
我的猜测是因为除了“私有”之外没有声明节点类?
虽然我不确定如何解决这个问题,因为我必须在 BST 课程的其余部分使用 Node*。
请让我知道我能做些什么:)
(我也搜索了另一个答案,但我发现的一切都与不同的
这是我的 BST 课程:
#ifndef BST_H
#define BST_H
#include <string>
using namespace std;
//DO I HAVE TO CAPITALIZE ALL 'CONSTANTS' PER THE PROG. ASSIGNMENT EXPECTATIONS?
//DO CLASS OBJECTS HAVE TO BE CAPITALIZED?
class BST
{
public:
BST();
~BST();
void deleteSubtree(Node* curr_root);
void insertContent(const string& word, const string& definition);
void deleteContent(string* word);
const string* getContent(const string& word);
Node* theRoot();
//WHY DOES COMPLILING SAY NODE HAS NOT BEEN DECLARED?
Node* treeMinimum(Node* ptr);
void nodeTransplant(Node* oldN, Node* newN);
private:
class Node
{
public:
Node(Node* cParent, string* word, string* definition)
{parent=cParent; left=NULL; right=NULL; m_word=word; m_definition=definition;}
Node* parent; //IS IT OKAY THAT I ADDED IN A PARENT ATTRIBUTE TO NODES?
Node* left;
Node* right;
string* m_word;
string* m_definition;
};
Node* root;
};
另外,我想知道如何在另一个类中几乎做同样的事情(其中带有 Node* 的行返回错误,指出尚未声明 Node)
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <string>
#include "BST.h"
using namespace std;
class Dictionary
{
public:
Dictionary();
~Dictionary();
void add(const string& word, const string& definition);
void remove(const string&);
const string* getDefinition(const string& word);
Node* getRoot();
void printEntry(Node* ptr);
void printInOrder(Node* ptr);
void printPreOrder(Node* ptr);
void printPostOrder(Node* ptr);
private:
BST dictionary;
};
#endif