Solved

missing storage-class or type specifiers

Posted on 2004-09-09
4
266 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
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
stefan73 earned 30 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 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