class myclass{
private:
int a;
public:
staticint b;
staticint 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?
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
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:
class myclass{
private:
int a;
public:
staticint b;
staticint 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)
class myclass{
private:
int a;
public:
staticint b;
staticint 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