Arrays in Classes

I am starting to learn classes, but Can not seem to get an array to work in my class.

(18):error C2059: syntax error : '{'
(18):error C2334: unexpected token(s) preceding '{'; skipping apparent function body
(14):error C2065: 'ABC' : undeclared identifier


Line 18: char ABC[3]= {'A','B','C'};
Line 14: for(int i = 0; i < 3; i++) cout << ABC[i] << endl;

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
#include "stdafx.h"
#include <iostream>
using namespace std;

class Bot
{
public:
	Bot()
	{
	}

	void Print()
	{
		for(int i = 0; i < 3; i++) cout << ABC[i] << endl;
	}

private:
	char ABC[3]= {'A','B','C'};
};

int main()
{
	Bot Main;
	Main.Print();
	system("pause");
	return 0;
}
What compiler are you using?
The initialization syntax on line 18 is only valid in a C++11 compliant compiler.
I am using Microsoft Visual Studios 2012.
Last edited on
I still can not get it to work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Bot
{
    public:
        Bot()
        {
            ABC[0] = 'A' ;
            ABC[1] = 'B' ;
            ABC[2] = 'C' ;
        }

        void Print() const /* const added */
        {
            for(int i = 0; i < 3; i++) std::cout << ABC[i] << /*endl*/ '\n' ;
        }

    private:
        char ABC[3] /* = {'A','B','C'} */ ;
};
Thank you. I got it to work.
Topic archived. No new replies allowed.