Array Data private member problem

I have an array within a class as a private member. I am trying to make a constructor to intilize this array, but I can only seem to make a new array within the constructor = useless. I need to find out how to use my array that is a private data member. Here's my code maybe I did something wrong since I haven't done this in awhile. Checked the forums kinda vague.


1
2
3
4
5
6
7
8
9
10
11
12
class PlayerMap 
{
      
private:
        
        int tileValue[];
        int row;
        int column;
        int playerLocation;
        int mapPath[];
        bool mapTiles[5][6];


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  int row = 0;
  int column = 0;
  row = 5;
  column = 6;

    bool mapTiles[row][column]; // x = 5 y = 6     
    int tileValue[30] = {0};

    int playerLocation = 0;
    playerLocation = 17;

    int counter = 0;
    int mapcounter = 0;
    
  int mapPath[] = { 1,2,6,8,9,11,12,15,16,17,18,19,20,21,23,25,29 }; // 17 total, 0-16 array
  


see I have quite a few arrays. One multi dimensional bool, Two intgers.

This is the only code I could get to work, but this won't do because it doesn't update the data members it only creates new arrays within the constructor which will go out of scope when it ends. Any help would be great. Thank you

If you can make this more efficient that would be great. Thanks
bool mapTiles[row][column]; // x = 5 y = 6
Bad, this will not work. You will need to dynamically allocate this array:

bool* mapTiles = new bool[row][column];
Also remember to delete it at the end of your program.

1
2
3
4
5
6
7
int row = 0;
int column = 0;
row = 5;
column = 6;
//...
int playerLocation = 0;
playerLocation = 17;

Umm...what? Seriously...

Also, what does your constructor look like?
So, the problem is that you don't know how to initialize the array, or that you want the array to have a size defined at run time?
memset(this->mapTiles,0,5*6*sizeof(int));
for the former.

[Code removed.]

EDIT: Damn. What a fool. Why didn't I ever think of that?
Last edited on
 
int mapPath[] = { 1,2,6,8,9,11,12,15,16,17,18,19,20,21,23,25,29 }; 


doing this creates a new array. It doesn't update the member. I need these values to be given to the data member array.

 
bool mapTiles[5][6];


I need a bool multi-dimensional array which should be defined & intilized in the constructor. 5 and 6 are just variables I picked I'd prefer if I didn't need to chose them till the constructor, but the compilers say the member needed bounds so I added 2 numbers.
Last edited on
For the first part, you can initialize a different array like that (with a different name) and copy the data to the one you need.

To create an array with a size determined at run time, use the method firedraco suggested.
1
2
3
4
5
// data member 
bool mapTiles;

// in constructor 
 bool* mapTiles = new bool[row][column];


Gives me a error

PlayerMap::column' cannot appear in a constant-expression

I've tried many different things but it still isn't working. I even broke out my old C++ book and cannot seem to find out how to set attributes to a multi dimensional data member which is all I really want. I solved the other problems using the books.

And
 
memset(this->mapTiles,0,5*6*sizeof(int));


frezzes the program when I run it.
Well, you data member needs to be a * for starters...here is an example:

1
2
3
4
5
6
7
8
9
10
11
class MyClass {
public:
   //cause i'm lazy
   bool* mapTiles;
   MyClass(int x_size, int y_size) { //get the data
      this->mapTiles = new bool[x_size][y_size]; //create the table thing
   }
   ~MyClass() {
      delete mapTiles; //don't forget to delete it!!
   }
}
Hey, it's true!
I was right, you can't allocate arrays like that!

Here's an example that does work:
1
2
3
4
5
6
7
int main(){
	int x=4,y=5;
	int **v=new int*[x];
	for (int b=0;b<y;b++)
		v[b]=new int[y];
	return 0;
}
Topic archived. No new replies allowed.