0

In the following code:

class Class
{
private:
LUID luid;

public:
Class()
{
luid = { 0, 0}; // A. Does not compile
LUID test = {0, 0}; // B. Compiles
test = {1,1}; // C. Does not compile
}

Why are A and C not right, but B is fine?

The error I get for A and C is:

error C2059: syntax error : '{'

error C2143: syntax error : missing ';' before '{'

error C2143: syntax error : missing ';' before '}'

I think it has to do with the C++ version, although I am not sure which version this is using besides it not being very new.

3
  • Well then time to find out what compiler you are using (name, version number, etc.)? Commented Oct 11, 2017 at 18:06
  • 3
    How is LUID defined? Do you have C++11 or higher turned on? Commented Oct 11, 2017 at 18:07
  • @UnholySheep The only common C++ compiler that outputs errors in that format is MS VC++. Commented Oct 11, 2017 at 18:45

1 Answer 1

3

Statement LUID test = {0, 0} is an initialization of a local variable using an initialization list; this is valid, as it is used in the course of a variable definition. test = {0, 0}, in contrast, is an assignment, as test is defined elsewhere. Assigning initializer lists is supported only in particular cases (e.g. when assigning to a scalar or to a particular sort of class type (cf., for example, braced-init-list assignment).

Other cases, like, for example, arrays, cannot be assigned but just initialized:

typedef int LUID[2];

int main(){

    LUID t = { 10, 20 }; // compiles
    // t = { 10, 20};     // does not compile, since an array is not assignable

     return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

That doesn’t explain why it was giving a syntax error. It’s valid syntax even if it isn’t valid to do it for the LUID class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.