0
votes

When I run my program, following error occurs each time when I use temp->.

[Error] request for member 'key' in '* temp', which is of pointer type 'NodeType {aka Node*}' (maybe you meant to use '->' ?)

What is wrong with the code.

struct Node;
typedef Node *NodeType;

int NumNodes = 0;
const int SIZE = 100;
NodeType data[SIZE];


struct Node{
int key;
NodeType *left, *right;
};

NodeType *newNode(int value){
  if(NumNodes == SIZE)
    cout << "This Node Pool is full." << endl;
    exit(1);

  NodeType *temp = &data[NumNodes++];
  temp->key  = value;                  
  temp->left = temp->right = NULL;     
  return temp;
}
3
which part of the error you dont understand? You managed to confuse yourself with the typedef. Remove it and the mistake should be crystal clear463035818_is_not_a_number
In practice avoid typedef-ing pointers (like you did for NodeType). It makes your code unreadable. Explicitly code Node* when needed. BTW, your error happens when you compile your program. Do enable all warnings and debug info when compiling (e.g. g++ -Wall -Wextra -g with GCC). Learn to compile on the command line. Read How to debug small programsBasile Starynkevitch
when i remove typedef it gives an error NodeType does not name a typemuzzi
... I didnt mean "just remove the line" but replace NodeType with Node* and then remove the typedef ....463035818_is_not_a_number
It is now giving the same error for Nodemuzzi

3 Answers

1
votes

You got yourself confused with pointers. First get rid of NodeType and use Node* instead. It's important to know where the pointers in your program are, hiding them behind a typedef doesn't do you any favours.

Now the real error. You're trying to create a pool of objects for allocation. so the pool should be objects not pointers.

int NumNodes = 0;
const int SIZE = 100;
Node data[SIZE];

Now you allocation function should return a pointer to an object in the pool. In your code it returns NodeType* which is a pointer to a pointer (because NodeType is a pointer). Also very importantly you have missing braces around your if statement in newNode (the call to exit is not inside the if statement in your code). So instead

Node *newNode(int value) {
    if (NumNodes == SIZE) {
        cout << "This Node Pool is full." << endl;
        exit(1);
    }
    Node *temp = &data[NumNodes++];
    temp->key  = value;                  
    temp->left = temp->right = NULL;     
    return temp;
}

Finally your struct also has pointers to pointers when it should just have normal pointers

struct Node{
    int key;
    Node *left, *right;
};

Basically you created a lot of confusion in your own mind by declaring NodeType as a pointer and then adding another pointer on top.

1
votes

If we get rid of the confusing typedef, this is what you have:

struct Node;

int NumNodes = 0;
const int SIZE = 100;
Node* data[SIZE];

struct Node{
    int key;
    Node **left, **right;
};

Node** newNode(int value){
  if(NumNodes == SIZE)
    cout << "This Node Pool is full." << endl;
    exit(1);

  Node** temp = &data[NumNodes++];
  temp->key  = value;                  
  temp->left = temp->right = NULL;     
  return temp;
}

As you can see, you have added a (needless) level of indirection, storing pointers to pointers in the tree, and having a pool of Node pointers instead of a pool of Nodes.

Remove the typedef and use Node insted of NodeType:

struct Node{
    int key;
    Node *left, *right;
};

int NumNodes = 0;
const int SIZE = 100;
Node data[SIZE];


Node* newNode(int value){
  if(NumNodes == SIZE) {
    cout << "This Node Pool is full." << endl;
    exit(1);
  }

  Node* temp = &data[NumNodes++];
  temp->key  = value;                  
  temp->left = temp->right = NULL;     
  return temp;
}
1
votes
struct Node
{
int key;
Node *left, *right;
};

int NumNodes = 0;
const int SIZE = 100;
Node data[SIZE];

Node* nNode(int value)
{

if(NumNodes == SIZE)
{
    cout << "This is full" << endl;
    exit(1);
}
else
{        
    Node* temp = &data[NumNodes++];
    temp->key  = value;                  
    temp->left = temp->right = NULL; 
    return temp;
}
}