我正在尝试创建一个Player对象的二叉搜索树。我之前已经定义了Player类。但是,当我在BST的每个节点的结构中包含播放器对象时,我得到一个错误,即玩家未定义,即使我认为我已经正确设置了包含。我有什么方法可以解决这个问题无需重新考虑我的实施?
我简化了一些代码来演示:
BST标题:
class Player;
class BinarySearchTree{
private:
struct Node {
Player info;
Node* left;
Node* right;
};
Node *root;
void Insert(Node*& tree, Player p);
void PrintTree(Node* tree, std::ostream& out);
};
BST.cpp
#include "Player.h"
#include "BinarySearchTree.h"
//all methods implemented afterwards
Player.h
class Player{
private:
std::string* name = new std::string;
int* score = new int;
public:
//....
};
Player.cpp
#include "Player.h"
//...
答案 0 :(得分:1)
编译器需要在BST.cpp中提供的头文件(BST.h)中查看class Player
的定义(通过包含" Player.h")。
因此,BST标题应为: -
#include "Player.h" <<<include this file
class Player; <<<remove this forward declaration
class BinarySearchTree
{
private:
struct Node {
Player info;