1

I'm battling with this assignment :)

I've got two classes: Ocean and Grid.

When I declare an object of the Grid inside the Ocean:

unsigned int sharkCount;
Grid grid;

The compiler/complainer says:

error C2146: syntax error : missing ';' before identifier 'grid'


Can you possibly predict what produces this error with the limited info I provided?

It seems that as if the Ocean doesn't like the Grid class. Could this be because of the poor implementation of the grid class. BTW the Grid has a default constructor.

Yet the error happens in compiling time!.

EDIT: They're each in separate header file, and I've included the Grid.h in the Ocean.h.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
m4design
  • 2,112
  • 3
  • 21
  • 28
  • 3
    Have you also included Ocean.h in Grid.h by mistake? That might cause a problem. – James Hopkin May 22 '10 at 06:51
  • @James Ok, I'm not gonna program anymore, because I did that :) Anyways, this doesn't fix my problem. Edit: actually it did, thanks a lot James. I'll try to figure out why is this problem is exactly happening. – m4design May 22 '10 at 06:55
  • This happened because of the order they were included. Remember that at the top of each header is an *include guard* which prevents itself from being included twice. Otherwise the headers would recursively include each other forever. – rlbond May 22 '10 at 07:11
  • Is the code you posted pasted verbatim from your code? Did you check you're not missing a semicolon just above the `Grid grid;` definition? – wilhelmtell May 22 '10 at 07:15

3 Answers3

2

My first guess would be that the definition of Grid simply isn't visible at the point that you've tried to use it in Ocean. Typically this happens if you have each in its own file, and haven't used a header to allow each to be "seen" by the other.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
2

We need the rest of the source, but there are a couple of possible answers:

  • Grid is being overloaded by something via a macro (#define)
  • the Grid class definition isn't being included.
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
1

In order for Grid to be used in your other class you either need to include the header of the Grid in the header or do a forward declaration of the class and declare it as a pointer in your class then instantiate it in your .cpp file.

class Grid;

class Ocean {
   Grid* grid;
};

or preferably :

class Ocean {
    boost::shared_ptr<Grid> grid;
};
AndersK
  • 35,813
  • 6
  • 60
  • 86