Initialise array in struct - incompatible type error

Hi,

I'm fairly new to C++, and I'm running into a problem with initialising arrays in structs.
My struct contains two arrays, but when I initialise the struct I keep getting the error:
"incompatible types in assignment of 'float*' to float [3]"
Similar error for the double array. Both arrays have a fixed size as indicated in their declaration.
I already found that I cannot assign an array in a struct (which I find weird, but okay), but I should be able to initialise it - that is at least the common answer found online. But this apparently does not work in my case.

Both structs have a specific initialiser (see code), to account for several variables that will be filled with real-time data later on in the code.

Please note: I'm using Eclipse to program for an Arduino application, so I won't have all the C++ functions like a vector available. I figured this is a general C++ question though.

Any idea how to solve this error?

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
struct A {
	String a;
	int b;
	float c;
	
	A(String A, int B) : a(A), b(B), c(0.0) {}
}

struct B {
	String name;
	A &a; // reference to structure of type A
	float p [3];
	double q [2];
	double x;
	
	B(String Name, A &aa, float P[3], double q[2]) : name(Name), a(aa), p(P), q(Q), x(0.0) {}
	
}

A aa[2] = 	{	{"name1", 1},
			{"name2", 1}
		};
				
B bb[2] = 	{	{"b1", &(aa[0]), {0.5, 1.0, 1.5}, {1.0, 2.0}},
			{"b2", &(aa[0]), {0.5, 1.0, 1.5}, {1.0, 2.0}}
		};
Last edited on
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
#include <iostream>
#include <string>

struct A {

	std::string a;
	int b;

	float c = 0.0 ; // default-member-initialiser
};

struct B {

	std::string name;
	A& a; // reference to structure of type A
	float p[3] ;
	double q[2] ;

	double x = 0.0 ; // default-member-initialiser
};

int main() {

    A aa[2] = {

                { "name1", 1 },
                { "name2", 2 }
    };

    B bb[2] = {

        { "b1", aa[0], {0.5, 1.0, 1.5}, {1.0, 2.0} },
        { "b2", aa[1], {0.5, 1.0, 1.5}, {1.0, 2.0} }
    };
}

http://coliru.stacked-crooked.com/a/a9c642e7d46e8a28
Thanks for the reply.
I had exactly that code earlier, but I kept getting the erorr:
error: could not convert '{"name1", 1}' from '<brace-enclosed initializer list>' to 'A'
And again the same error for all B structs.

I have ensured that the default members are last in the list so that the order of the initialisation follows the order of declaration. To what I found previously (that's why I switched to using the constructor), newer GCC versions do not allow for default member initialisation.
Last edited on
This compiles cleanly with GCC 5.1 or above and the compiler option -std=c++14

--------------------------- g++ 7.1 -std=c++14 ------------------------------------
g++ (GCC) 7.1.0
ok
--------------------------- g++ 5.1 -std=c++14 ------------------------------------
g++-5.1 (GCC) 5.1.0
ok
--------------------------- g++ 5.1 -std=c++11 (error) ------------------------------------
g++-5.1 (GCC) 5.1.0
main.cpp:15:5: warning: non-static reference 'A& B::a' in class without a constructor [-Wuninitialized]
  A& a; // reference to structure of type A
     ^
main.cpp: In function 'int main()':
main.cpp:28:5: error: could not convert '{"name1", 1}' from '<brace-enclosed initializer list>' to 'A'
     };
     ^
main.cpp:28:5: error: could not convert '{"name2", 2}' from '<brace-enclosed initializer list>' to 'A'
main.cpp:34:5: error: could not convert '{"b1", aa[0], {5.0e-1, 1.0e+0, 1.5e+0}, {1.0e+0, 2.0e+0}}' from '<brace-enclosed initializer list>' to 'B'
     };
     ^
main.cpp:34:5: error: could not convert '{"b2", aa[1], {5.0e-1, 1.0e+0, 1.5e+0}, {1.0e+0, 2.0e+0}}' from '<brace-enclosed initializer list>' to 'B'

http://coliru.stacked-crooked.com/a/e0c5605f6161ee41
As said, I'm developing for Arduino in Eclipse. My GCC is version 4.9.2, this comes with the Arduino plugin by default. There is probably a way to update it, but I haven't looked into that.

I have solved it by now by simply skipping using default member initialisation and initialising them as 0.0 myself, see code below. It's by far not an elegant solution, I fully realise that, but it's the best for now I guess.. At least I understand now why the alternatives do not work. Thanks!

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
struct A {

	std::string a;
	int b;
	float c;
};

struct B {

	std::string name;
	A& a; // reference to structure of type A
	float p[3] ;
	double q[2] ;

	double x;
};

int main() {

    A aa[2] = {

                { "name1", 1, 0.0 },
                { "name2", 2, 0.0 }
    };

    B bb[2] = {

        { "b1", aa[0], {0.5, 1.0, 1.5}, {1.0, 2.0}, 0.0 },
        { "b2", aa[1], {0.5, 1.0, 1.5}, {1.0, 2.0}, 0.0 }
    };
}
Topic archived. No new replies allowed.