container questions and probems.

Hi!
I'm quite green so I might have formulated the question wrong.
I'm using a Class called "Cell" to save all the information i need to have about one cell on the map. However a map consists of 250k Cells and therefor I needed a way to create them.

I solved it by making this line of code:
1
2
const int NUM_CELLS = 250000;
Cell cellMap[NUM_CELLS];


The problem is that the functions used to get/set the Cells don't work unless I declare it in the global (scope?). Therefor I tried to make a class called Map that holds a vector that can contain the Cells. This is my attempt:
1
2
3
4
5
6
7
8
9
10
11
12

class Map
{
      public:
             Map(int spaces = 1) { m_Cells.reserve(spaces); }
             void Create();
             void Save();
             void Load();
             
             
      private:
              vector<Cell> m_Cells;

It won't compile since it forbids declaration of a vector object without a type. However in the example I am using as a reference I can't find the difference.




Example:(from beginning C++ game programming chapter 9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//Critter Farm
//Demonstrates object containment

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Critter
{
public:

    Critter(const string& name = ""): m_Name(name) {}
    string GetName() const { return m_Name; }  
private:
    string m_Name;
};

class Farm
{
public:
    Farm(int spaces = 1) { m_Critters.reserve(spaces); }
    void Add(const Critter& aCritter) { m_Critters.push_back(aCritter); }
    void RollCall() const
    {
        for (vector<Critter>::const_iterator iter = m_Critters.begin(); 
             iter != m_Critters.end(); ++iter)
            cout << iter->GetName() << " here.\n";
    }
private:
    vector<Critter> m_Critters;
};
Last edited on
Make sure you've declared Cell before you declared Map.

1
2
3
4
5
6
7
8
class Cell
{
//...
};
class Map
{
//...
}

Should work.

The compiler only knows what it's already read, so if you declare Map before declaring Cell, the compiler won't know what Cell is while trying to compile Map.
Did that... still complains. Entire file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*The hexes contain functions to change the values of the variables available
buildings pop */
#include <iostream>
#include <vector>
using namespace std;
const int NUM_CELLS = 250000;

class Cell
{
          
      public:
             //constructor that requires (int, string) otherwise values as below
             Cell::Cell(int a = 1000, string b = "13212"){
             m_pop = a;
             m_buildings = b;
             }
      
             //static int s_Total = 0; //static data member
             //get and set
             int Getpop();
             string Getbuildings();
             void Setpop(int);
             void Setbuildings(string);
             // operations
             void Create();
             void Load();
             void Save();
      private:
              //data
              //*string m_powner;
              int m_pop;
              string m_buildings;
              };
              
class Map
{
      public:
             Map(int spaces = 1) { m_Cells.reserve(spaces); }
             void Create();
             void Save();
             void Load();
             
             
      private:
              vector<Cell> m_Cells;
ah, had missed row 38 Map::Map
next problem... "Cell.cpp - cannot declare member function `Cell::Getpop' within `Map' "

suggestions?
ok, solved almost all problems except that there seems to be a problem with this part:
1
2
3
4
5
6
7
8
9
vector<Cell>::const_iterator iter = m_Cells.begin(); 
          while(getline(Mapfile,line))
          {
                    stringstream(line) >> a;
                    iter->Setpop(a);
                    getline(Mapfile,line);
                    iter->Setbuildings(line);
                    ++iter;
          }
Last edited on
ok, solved it all Thanks for the help!
Topic archived. No new replies allowed.