struct is not working, I tried making a tree

Could you please explain to me what exactly am I doing wrong? I followed the tutorials and it still gives me an error.
Thanks in advance :D

#include <iostream>

using namespace std;

typedef struct tree {
int x;
struct tree *parent;
struct tree *left;
struct tree *right;
} tree;

int main()
{
tree.x=4; - here is my error it says unqualified token before '.'
return 0;
}
Last edited on
tree is a type, not an instance variable you can use.

You have two things called 'tree' - one is "struct tree" and the other is a typedef name 'tree' which refers to 'struct tree'

You could remove the word typedef, which is redundant in C++ when it comes to declaring structs and classes.
Then you end up with a global variable called 'tree' which is of type 'struct tree'.

Topic archived. No new replies allowed.