how to init static 2d arrays of a class

Hi guys,
i have written a class:

1
2
3
4
5
6
7
8
9
10
11
class myclass{
private:
	int a;
public:
	static int b;
	static int c[10][10];

	myclass(){
		a = 0;
	}
};


i have couple of questions:
1. when i initialize b outside main using int myclass::b = 4; i get no erros
but when i use the same inside main i get this error: "error C2086: 'int myclass::b' : redefinition" why is that?


2. ok even if the above works, i am not able to initialize c at all (either outside or inside main)
like writing "int myclass::c[2][2] = 23;" gives error:
'initializing' : cannot convert from 'int' to 'int [2][2]'
does that mean i cant declare 2d static arrays?
is there any other way of making an array that is common to all objects of that class?

thanks.
Last edited on
1. because of the modifier myclass::, if you remove it, like this: int b=0; you will get no error too. but if you did this, it means you define a local variable in the main, not the initilization of the variable b in the myclass.

2. int myclass::c[2][2] = {{23,23}}; here is a correct example to intilize the c[0][0] to (23,23). hope you got that already.
Your code doesn't make sense. You're trying to assign values to a Class variable (which doesn't exist). Only Objects of a class have variables. Before you can manipulate those values, you have to instantiate an Object of that Class.

1
2
myclass myObject = myObject();
myObject.b = 4;

That'll work.

In the case that class-variable-assigning does for some reason work, you'd still have to drop the 'int' part of your statement. You only use that for declaring new variables.
I think you need the global int myclass::b = 4; to define the variable.

If you have done this in global scope you can then access the variable in main using myclass::b = 3; (whatever)
(since this is effectively a global variable and you have made it public in the class)

chinalux is right r.e. the global myclass::c variable. You need to use C style array initialisation

thanks guys,
using this works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream.h> 
#include <stdafx.h>
using namespace std; 

class myclass{
private:
	int a;
public:
	static int b;
	static int c[2];

	myclass(){
		a = 0;
	}
};


int myclass::b = 4;
int myclass::c[2] = {1,2};

int main(){
	myclass ob;
	
}



but i dont understand why i must define those variables b and c outside main.. why does it give me an error if i define them inside main.
When you have static data in a class this is just the same as global data.
(except the name is myclass::b instead of just b)

Global data is all initialised before main even starts. If fact the variable int myclass::b = 4; will already have a space in the
executable file with its initialised value.

Thats why you get a redefinition error when you try to create the variable in main.
ok I understood that. but what if my static array 'c' was a huge one, of say size 100, and i did not wish to initialize it directly using {1,2,3,....100} but instead wanted to use a for loop.
how will i do that?

i obviously cant use a for loop outside main to initialize the array, so i will have to use a function like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class myclass{
private:
	int a;
public:
	static int b;
	static int c[2];

	myclass(){
		a = 0;
	}
};


void func(){
	int myclass::b = 4;
	for(int i = 0 ; i < 100; i ++)
		int myclass::c[i] = 5;
}

int main(){
	myclass ob;
	func();
	
}


but as you might expect i get errors in this too.. (redefinition and cant convert int to int[] etc)
In C if there are fewer initialisers then the array size then it is filled with zeros

int c[100] = { 0 };

should give you an array of 100 zeros

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
class myclass{
private:
	int a;
public:
	static int b;
	static int c[100];

	myclass(){
		a = 0;
	}
};

// need to declare globally
int myclass::b = 0;
int myclass::c[100] = { 0 };

void func(){
	myclass::b = 4;  // access global variable
	for(int i = 0 ; i < 100; i ++)
		myclass::c[i] = 5; // access global variable
}

int main(){
	myclass ob;
	func();
	
}


You only initialise once in the global space. After that you can change the value programmatically if you need to
Thank a lot mik2718! that answers my question!
Topic archived. No new replies allowed.