views:

194

answers:

6
+10  A: 

Use pointers. Node* previous; would solve the problem.

As you're doing it now, you actually try to make your class infinitely large.

Kotti
+1  A: 

Shouldn't Node be a pointer?

illuzive
+8  A: 

Use a pointer or a reference.

For example:

Node* previous;

The only restriction is that a class cannot have an actual field of itself, likely for construction reason. Think about it, if every Box contains another Box inside it, how would you end up with a finite number of boxes?

If I'm not mistaken, C# only uses references for class types (as does Java), so you're using a reference there, just not seeing it.

Uri
+2  A: 

In C#, declaring previous like that gives you a reference. In C++, declaring previous like that gives you an object, embedded in each object of class Node.

An object can't have an object of the same class embedded in it, recursively. previous.name would have to exist (and have storage allocated to it), as would previous.previous.name, and so on infinitely, all in a single memory allocation. The size of a Node object would have to be at least as much as the size of a string object plus the size of a Node object (which is impossible since string is more than 0 bytes). And other such contradictions.

As others say, use a pointer: Node *previous or a reference Node &previous. Or a std::deque<std::string> ;-)

You might like to know that C++ differs from C# in other ways which will spoil your day if you're trying to write a singly-linked list by porting C# code. No garbage collection, for example. You can't write correct C++ code without understanding the C++ object model.

Steve Jessop
Thanks for the detailed explanation. I am re-learning C++,haven't used since long time :)
TheMachineCharmer
+5  A: 

For the edit, think about it, your node contains another node into it which contains another one inside. So you have an infinite sequence of nodes contained in one another. What you want is a pointer to the location where the next node is stored. So you should change it to Node*.

Naveen
+2  A: 

Think about a Car object "in the real world". It can contain a reference to another Car object via the VIN number. It could even contain a reference to itself, or to the VIN value of 0 (NULL, or NO CAR).

But what if you literally wanted another Car OBJECT inside every Car Object? How would that work? What Car object would the containing Car object contain?

Java and C# don't actually place THE OBJECT inside the containing class, but a REFERENCE to the object. Languages like C++ allow you to specify whether there is an OBJECT contained within the class, a REFERENCE to another OBJECT contained within the class, or even a POINTER to another OBJECT contained within the class:

class Car
{
   Engine engine;  // Car CONTAINS an Engine object
};

class Car
{
   Engine &engine;  // Car contains a REFERENCE to an Engine object
};

class Car
{
  Engine *pEngine;  // Car contains a POINTER to an Engine object
};

Eventually you'll get to the point where you know when and why to use the first, the second, or the third, but that's a different question altogether.

franji1
+1 absolutely brilliant use of Car ;)!
TheMachineCharmer