Solved

missing storage-class or type specifiers

Posted on 2004-09-09
4
286 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 141
Template syntax for variable length arrays 9 75
best sources to up-to-date in C++? 8 91
Precision Problem in C++ 7 44
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

821 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