Reputation: 9614
I have the following code:
#pragma once
class Matrix{
public:
Matrix();
~Matrix();
protected:
float mat[3] = {0.0, 0.0, 0.0};
};
but I'm getting an error on the float mat[3] = {0.0, 0.0, 0.0};
. It says Error C2059: syntax error : '{' and error C2334: unexpected token(s) preceding '{'; skipping apparent function body.
I am create the array correctly aint I? What is the problem then?
Upvotes: 6
Views: 305
Reputation:
C++ did not support non-static data member initializers until after C++11 standard was ratified. In order to use this feature, you must have a compiler that supports C++11. Also, it is often disabled by default, so you will probably need to enable it manually. For GCC, specify std=c++11
. For Clang, do -std=c++11 -stdlib=libc++
. If you use something else, check the documentation.
Upvotes: 7
Reputation: 726479
C++03 does not support inline initialization of member fields. You need to move this initialization into the constructor, for example (link to a demo):
class Matrix{
public:
Matrix() : mat({0.0, 0.0, 0.0}) {};
~Matrix();
protected:
float mat[3];
};
The above defines the constructor inline; if you define the constructor separately, move the initialization list (i.e. the code between the colon :
and the opening brace {
) together with the constructor definition.
Upvotes: 11