error: incompatible types in assignment of 'A* const*' to 'A* const [0]'

I am trying to initialize an array of pointers.
Line 18 below has a syntax error. How can it be fixed?

Thank you.

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
30
31
32
33
34
35
36
37
#include <iostream>

class A
{
	private:
		const char c;
	public:
		A(const char c): c(c) {}
		void print() { std::cout << c << std::endl; }
};

class B
{
	private:
		A*const prtsA[];
	public:
		//error: incompatible types in assignment of 'A* const*' to 'A* const [0]'
		B(A*const pa[]): prtsA(pa) {}
		//                       ^
		void printArray()
		{
			prtsA[0]->print();
			prtsA[1]->print();
			prtsA[2]->print();
		}
};

int main()
{
	A x('x');
	A y('y');
	A z('z');
	A*const prtsA[] = {&x, &y, &z};

	B b(prtsA);
	b.printArray();
}

array_constructor2.cpp: In constructor 'B::B(A* const*)':
array_constructor2.cpp:17:28: error: incompatible types in assignment of 'A* const*' to 'A* const [0]'
   B(A*const pa[]): prtsA(pa) {}
                            ^
Line 15 is not valid C++, you must specify the size of an array.

Why are you trying to use C-style arrays when you have access to C++? Learn to use a std::vector:
http://www.cplusplus.com/reference/vector/vector/
http://en.cppreference.com/w/cpp/container/vector
Hi LB,
I am using C-style arrays because this is for Firmware on an ATMEGA32U4, which does not have access to std::vector.

I put in the array size as you suggested, but still get the same compile error on line 17.
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
30
31
32
33
34
35
36
#include <iostream>
class A
{
	private:
		const char c;
	public:
		A(const char c): c(c) {}
		void print() { std::cout << c << std::endl; }
};

class B
{
	private:
		A*const prtsA[3];
	public:
		//error: incompatible types in assignment of 'A* const*' to 'A* const [0]'
		B(A*const pa[]): prtsA(pa) {}
		//                       ^
		void printArray()
		{
			prtsA[0]->print();
			prtsA[1]->print();
			prtsA[2]->print();
		}
};

int main()
{
	A x('x');
	A y('y');
	A z('z');
	A*const prtsA[] = {&x, &y, &z};

	B b(prtsA);
	b.printArray();
}
Arrays can't be copied like that anyway, so my suggestion was in vain. You'll need to initialize the array elements individually.
Sorry I misstated by intent. I want to initialize B::prtsA to point to a pointer array.
Thanks for making me think about it more. I got it to work now:
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
30
31
32
33
34
35
36
#include <iostream>
class A
{
	private:
		const char c;
	public:
		A(const char c): c(c) {}
		void print() { std::cout << c << std::endl; }
};

class B
{
	private:
		A*const *const prtsA;
	public:
		//error: incompatible types in assignment of 'A* const*' to 'A* const [0]'
		B(A*const pa[]): prtsA(pa) {}
		//                       ^
		void printArray()
		{
			prtsA[0]->print();
			prtsA[1]->print();
			prtsA[2]->print();
		}
};

int main()
{
	A x('x');
	A y('y');
	A z('z');
	A*const prtsA[] = {&x, &y, &z};

	B b(prtsA);
	b.printArray();
}


output:
x
y
z
Topic archived. No new replies allowed.