Array error.

Hi all,

when i compile the code, it indicated error. can anyone help me me? i have no idea what when wrong with it.

1
2
protected:
	string Items[4] = {"aa", "bb", "cc", "dd"};


here is the error:
1. syntax error : '{'
2. unexpected token(s) preceding '{'; skipping apparent function body
Just declare
1
2
protected:
	string Items[4];


and then in constructor :

1
2
3
4
Items[0] = "aa";
Items[1] = "bb";
Items[2] = "cc";
Items[3] = "dd";
there is no much different, as i am still getting the same error in
1
2
3
4
Items[0] = "aa";
Items[1] = "bb";
Items[2] = "cc";
Items[3] = "dd";
Something like this would work I'd imagine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class YourClass
{
public:
    YourClass();
    ~YourClass();

protected:
    string Items[4];
};

YourClass::YourClass()
{
Items[0] = "aa";
Items[1] = "bb";
Items[2] = "cc";
Items[3] = "dd";
}
~YourClass::YourClass()
{
}
thanks, it works. but may i know what do we need to have it at constructor instead? did i create the array wrongly?
Topic archived. No new replies allowed.