initialize members of a class
i have a class
and i get an error
'A::a' : only static const integral data members can be initialized within a class
i get errors like this and if i write
const int a=0;
or
static int a=0;
or
static const int a=0;
any idea??
Unfortunately C++ does not yet support this very intuitive way of initialization. You have got to write is as follows:
1 2 3
|
class A{
A: a(0) {}
int a;}
| |
Sorry!
Member variables should be initialized inside a constructor, or using an initialization list:
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
|
#include <iostream>
using namespace std;
struct A
{
int a;
A() { a=0; }
};
struct B
{
int b;
B():b(1) {}
};
int main()
{
A a;
B b;
cout << a.a << endl;
cout << b.b << endl;
cout << "\nhit enter to quit...";
cin.get();
return 0;
}
| |
EDIT: Damn... Too slow...
Last edited on
ok got it..
thank you!!
Topic archived. No new replies allowed.