0

I have a class called Regions and another class I will call Area. In the main class Area, I need to have an array of Region class objects.

Here is my region class:

class Region 
{
public:

    // Constructor
    Region();

    // Get/set functions
    bool getPoly()              {return poly;}
    bool setPoly(bool value)    {poly = value;}
    long getMesh()              {return mesh;}
    void setMesh(long value)    {mesh = value;}
    long getVisibleNum()        {return visibleNum;}
    void setVisibleNum(long value)  {visibleNum = value;}

    // Visibility vector
    void reserveSpace();
    long addVisibleRegion(int region);
    long getSize(){return visibility.size();}

    friend class Area;

private:
    bool poly;          // Does the region have polygons?
    long mesh;          // The reference to a 0x36 mesh
    long visibleNum;    // The number of visible regions
};

Now in my area class, I am trying to declare something like this :

class Area
{
public: // Some public class functions
private:
Region* regionArray; // this should be pointers to an array of class objects
}

In the Area class constructor, I will allocate the number of class objects I want.

I am getting this error:

error C2143: syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int

So I assume I am not setting it up correctly. It worked perfectly when Region was a struct but now that it is a class, I assume I am doing something wrong.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055

4 Answers4

1

Yes it is possible - do you have the classes defined in the same file? It sounds like they might be in separate files and you haven't included the header file for Region in the Area file.

Edit: Also, you have getter functions for the data in Region, so there is no need to make Area a friend of Region. I would also replace Region* regionArray with std::vector<Region> regionArray, so the memory is all managed for me.

Node
  • 3,443
  • 16
  • 18
1

It seems to me that the problem is that you are referring to a class you have not yet defined. That is the: Region* regionArray; line in the Area class.

If this is the case you have to remember to include the header containing the Region class in the header file containing the Area class. Or add class Region; above the Area class.

Tommy Andersen
  • 7,165
  • 1
  • 31
  • 50
1

Yes, that is possible. In your case, the problem isn't array. but (most likely) you've not included some header files in some other files.

By the way, you can use std::vector as well, which in my opinion would be a better choice, in comparison to raw pointer.

#include <vector>
#include "Region.h" //must include these two

class Area
{
  public: // Some public class functions
  private:
  std::vector<Region> regions;  //better than raw pointer
};

Read about RAII Idiom : Resource Acquisition Is Initialization

Nawaz
  • 353,942
  • 115
  • 666
  • 851
0

Other common problem is the friend class Area; declaration inside the class without a forward declaration first (I'm almost sure this is the case, are you using GCC?)

In Region.h

class Area;  // <- this is a forward declaration

clasee Region {
 ...
 friend class Area;
};

Regards

user548569
  • 158
  • 2
  • 6