tags:

views:

429

answers:

1

I'm building a game and I was compiling seeing what sort of errors were coming up and there is one there is very common and very puzzling to me:

1>c:\users\owner\desktop\bosconian\code\bosconian\ship.h(9) : error C2460: 'Ship::Coordinate' : uses 'Ship', which is being defined

This also comes up for the SpaceObject class and all other derivations of it. The Coordinate class is simply just a 2d vector class and, if it matters, references nothing but an Environment class.

The Environment class references the SpaceObject's but I don't believe that's a problem (correct me if I'm wrong)

This is my first larger scale project in C++ and I was wondering if this might be a common newbie error with a more obvious solution. If it matters the SpaceObject class hierarchy is not all the way filled in, but it is filled in several levels past the SpaceObject and Ship classes. .
.
.
Edit: This is in response to the comment.

-When I say reference I mean refer to that class in another class. Like in the SpaceObject class I reference the Coordinate class:
Coordinate * position

Environment does make reference to SpaceObject pointers, but I can't see I could not reference classes like that...I mean, all my classes have to relate to eachother in some way, right?

In response to pasting code, it's sort of hard because they are huge classes but here is the line the error is pointing to:

public:
Ship(Coordinate * positionObject_, int direction_, int possibleDirections_, int maxHealth_, Component * objectSectors_, int numOfObjectSectors_, double speed_);//this is the line
void move();//handles the actual translation of calculated move on the map (i.e. bounds checking)
+11  A: 

You've got something like this:

class Ship
{
    class Coordinate
    {
        Ship m_ship;
    };

    Coordinate m_coordinate;
};

The problem is that each Ship object contains as a member a Coordinate, which contains as a member a Ship, ad nauseum. The size of a Ship would become infinitely large if this were allowed to continue. What you really want is to replace those member variables with pointers or references - a pointer/reference has a known size (e.g. 4 bytes on a 32-bit CPU), and can be declared without knowing any information about the type being pointed/referred to.

Adam Rosenfield
Hmmm...I think I understand what you're saying. So I'm really limited to using pointers (in most cases), because the compiler can't instantiate the objects because it doesn't know how much memory to allocate?
Chad
Chad, yes that's it. Think about it. A ship has-a coordinate. But a coordinate doesn't have a ship! it is just connected to a ship, so you need a pointer.
Johannes Schaub - litb
I would probably use references instead of pointers if they are never going to change. I would guess that you will never need to replace a ship's coordinate object.
Matthew Crumley
That is a common problem with circular dependencies. Try to avoid them whenever possible. When you cannot avoid them, your only solution, as noted above, is using either a pointer (raw/smart pointer) or a reference.
David Rodríguez - dribeas