Solved

missing storage-class or type specifiers

Posted on 2004-09-09
4
238 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
Comment Utility
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
Comment Utility
>>>> 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
Comment Utility
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
Comment Utility
>> 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now