?
Solved

missing storage-class or type specifiers

Posted on 2004-09-09
4
Medium Priority
?
321 Views
Last Modified: 2010-04-01
I created a header file including a Node Class and a
NodeList.

class Node{
  Node* next;
};

class NodeList{
 Node* first;
 Node* last;
};

I wanna declare a NodeList inside the Node but since
NodeList is declared after Node it's impossible!!!
like this...

class Node{
  Node* next;
  NodeList list;
};

I get:
error C2146: syntax error : missing ';' before identifier 'list'
error C2501: 'Node::list' : missing storage-class or type specifiers
error C2501: 'Node::NodeList' : missing storage-class or type specifiers

What do you do in such cases? I am familiar with Java but it seems that C++ is very capricious and does not allow this kind of things.

Help me plz...30 points is all I got :)
0
Comment
Question by:Trooper80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
stefan73 earned 120 total points
ID: 12016410
Hi Trooper80,
Use a forward declaration:

class Node;

class NodeList{
 Node* first;
 Node* last;
};


class Node{
  Node* next;
  NodeList list;
};

You can only use pointers to classes not yet defined. Probably you wanted to have
class Node{
  Node* next;
  NodeList* list;
};


Cheers!

Stefan
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12017772
>>>> I wanna declare a NodeList inside the Node

That makes no sense. A Node only has pointers to other Nodes. If you want to go backwards you should have a 'prev' pointer:

class Node
{
    Node* next;
    Node* prev;  // to iterate backwards
};

If you want to implement a tree, you may have that:

class Node
{
    Node* parent;   // to go up
    Node* left;       // to go left
    Node* right;     // to go right
    Node* first;      // to go down-left
    Node* last;       // to go down-right

public:
    Node() : parent(NULL), left(NULL), right(NULL), first(NULL), last(NULL) {}
};

Regards, Alex

0
 
LVL 1

Author Comment

by:Trooper80
ID: 12019228
Hey Alex thanks for the tip man but I know what I'm doing. Everynode will have a sub-set of nodes connected to it...so why not use a linked list again but this itme with only the nodes that are connected to it?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12019390
>> why not use a linked list

The class NodeList from above actually isn't a list, but has only two Node pointers, first and last. 'last' doesn't help as long as you have no 'prev' pointer in class Node and having only a 'first' pointer makes no sense as you could have the  'first' Node pointer in class Node instead of a NodeList (or NodeList pointer) with less overhead.

But, if you think you need that NodeList class, do it the way Stefan told you: Have a forward declaration of class Node, then define class NodeList using Node pointers (as they are only declared but not defined). Then, you can define class Node using either a NodeList pointer or a NodeList object as data member:

class Node;

class NodeList
{
     Node* first;
     Node* last;

public:
    NodeList() : first(NULL), last(NULL) {}
};

class Node
{
    Node*    next;
    Node*    prev;  // to iterate backwards
    NodeList list;

public:
    Node() : next(NULL), prev(NULL) {}
};

Regards, Alex

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question