i wana create a bst tree in c++ but i have a syntax error in this code :
#pragma once
#include "BSTNode.h"
using namespace std;
class BST
{
private:
BSTNode* root;
public:
BST(void);
bool insert(int );
int search(int);
~BST(void);
};
and BSTNode is:
#pragma once
#include "BST.h"
class BST;
class BSTNode
{
friend class BST;
private:
int data;
BSTNode * LeftChild, *RightChild;
public:
BSTNode(void);
int getData();
~BSTNode(void);
};
my error is:
Error 1 error C2143: syntax error : missing ';' before '*'
i think dont have error .please help me!
You have a circular include, with 2 files, and because of the #Pragma once, both files are only included once, and therefore BSTNode is parsed first and includes BST, but then BST is not including BSTNode anymore (because it is pragma once).
this leads to BST not knowing what a BSTNode is, solution would be:
Removing the include and forward declaring the class like such:
Example of main function: