1

I am newbie to developing in C++ for Windows..

I am trying to produce a struc which will have pointer to other struct...

How is this possible?

struct InitialNode {  
     Node * nextNode; 
     Node * lowerNode;
} InitialNode;

struct Node {  
     Node * nextNode; 
     Node * lowerNode;
     int value;
} Node;

An error is being highlighted at the InitialNode struct where the Node * nextNode; and Node * lowerNode are..

Any ideas?

Thanks a lot beforehand :)

cgval
  • 1,096
  • 5
  • 14
  • 31
  • This isn't specific to Windows, all C++ compilers have the same rules here. They all compile top to bottom, except for class methods (which are always compiled after the class itself) – MSalters Apr 19 '12 at 12:54

2 Answers2

4

There are several errors:

First, I'm pretty sure you wanted a typedef. Also, you need to forward-declare Node before InitialNode:

struct Node;
typedef struct InitialNode {  
     Node * nextNode; 
     Node * lowerNode;
} InitialNode;

typedef struct Node {  
     Node * nextNode; 
     Node * lowerNode;
     int value;
} Node;

Your syntax

struct A
{
} A;

attempts to create an object of type A named A. Note that the typedef struct is a C-style declaration and is not needed in C++. You could very well write:

struct Node;
struct InitialNode {  
     Node * nextNode; 
     Node * lowerNode;
};
struct Node {  
     Node * nextNode; 
     Node * lowerNode;
     int value;
};
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • typedef is not necessary in this case although it is recommended http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c – EdChum Apr 19 '12 at 11:13
  • 1
    @EdChum I can't see where it says it is recommended for C++. In fact there are several answers that state that for C++ it makes no difference. – Luchian Grigore Apr 19 '12 at 11:16
4

In c++ an identifier can be used only AFTER it's declared. Try this:

struct Node {  
     Node * nextNode; 
     Node * lowerNode;
     int value;
};

struct InitialNode {  
     Node * nextNode; 
     Node * lowerNode;
} InitialNode;

Defining Node before InitialNode should solve your problem

Emiliano
  • 22,232
  • 11
  • 45
  • 59