Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

missing storage-class or type specifiers

Posted on 2004-09-09
4
Medium Priority
?
334 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

Technology Partners: 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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

636 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