I'd previously used TDM-GCC-5.10 and now switched back to 4.9 MINGW-GCC and getting a weird error with trying to use list-initialization:
class Vector2
{
public:
Vector2(float x, float y)
{
this->x = x;
this->y = y;
}
float x = 0.f;
float y = 0.f;
};
struct Test
{
int x = 0;
Vector2 v;
};
int main()
{
Test tst = {0,Vector2(0.0f,0.0f)}; //Error
return 0;
}
Error:
main.cpp: In function 'int main()':
main.cpp:21:41: error: could not convert '{0, Vector2(0.0f, 0.0f)}' from '<brace-enclosed initializer list>' to 'Test'
Test tst = {0,Vector2(0.0f,0.0f)}; //Error
^
I used C++14 with both compilers. What is wrong?
The problem is here:
struct Test
{
int x = 0; // <==
Vector2 v;
};
Until recently, default member initializer prevent the class from being an aggregate, so you cannot use aggregate initialization on them. Gcc 4.9 still implements the old rules here, whereas gcc 5 uses the new ones.
You missed ;
after your class definition and after int x = 0
. Then you got many errors and apparently only considered the last one. But your compiler was confused because Vector2
was not defined (due to missing ;
).
This compiles:
int main()
{
class Vector2
{
public:
Vector2(float x, float y)
{
this->x = x;
this->y = y;
}
float x = 0.f;
float y = 0.f;
};
struct Test
{
int x;
Vector2 v;
};
Test tst = {0,Vector2(4,5)};
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With