error: incompatible types in assignment of 'A* const*' to 'A* const [0]'
Sep 17, 2014 at 1:47am UTC
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) {}
^
Sep 17, 2014 at 2:34am UTC
Sep 17, 2014 at 2:47am UTC
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();
}
Sep 17, 2014 at 2:53am UTC
Arrays can't be copied like that anyway, so my suggestion was in vain. You'll need to initialize the array elements individually.
Sep 17, 2014 at 3:27am UTC
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:
Topic archived. No new replies allowed.