6

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?

4
  • @chris really? even if it's not static const integral? Commented Mar 5, 2013 at 18:34
  • @Dave, Yup, look up in-class member initialization. Here's some proof. Commented Mar 5, 2013 at 18:34
  • Works in g++ 4.7.2 but not 4.6.3 Commented Mar 5, 2013 at 18:36
  • 1
    @ShafikYaghmour, Yes, the C++11 support was updated quite a bit in that time. Commented Mar 5, 2013 at 18:37

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I almost downvoted you for saying that C++ does not support it :) But it seems like your style is to post as soon as you start typing and then keep editing :)
@VladLazarenko You are correct, I usually post as soon as the answer becomes helpful, at least marginally. Quite often, that is more than enough to get the OP on the right track. In the remaining five minutes prior to the edit lock-up, I add more text for the potential readers who may find this post through Google.
@VladLazarenko Not to start a search engine war or anything, but "Why I can't create my array" is at the top of Google's search as of this writing, while the duckduckgo hasn't found it yet :( I guess that's an alternative way of saying "size matters".
For you, not for me though. And you know why? Because it knows very well what you are up to. The time when different people saw the same results in Google are over. DDG crowdsourcing Google as well, but results are context-free due to zero tracking.
@VladLazarenko You're probably right, they are tracking me! I didn't decide yet if it's good or bad, but it's certainly bordering on creepy.
|
7

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.