Luke
Luke

Reputation: 2400

Visual Studio 2012 C++ arrays initialization using { }

i've just started programming in visual studio 2012 Express and from the beginning I'm having problems with arrays.

The environment says that this code is invalid:

int a[10] = {5,1,8,9,7, 2,3,11, 20,15};

First of all i had to declare that this array has fixed size using fixed keyword, but after that the program still has been wanting to put ; after a[10]. Filling up this array one number by one would be waste of time. Is it possible to work around it? I can't find any solution in google so I decided to post my problem here.

Upvotes: 0

Views: 2486

Answers (2)

Daksh Gupta
Daksh Gupta

Reputation: 7804

May be its too late to but you can use STL array for fix size arrays as

#include <array>
std::array<int, 5> ary { 1,2,3,4,5 }

This will be a fixed size array

As mentioned by Marco A. there is no "fixed" keyword in C++

Upvotes: -1

Marco A.
Marco A.

Reputation: 43662

  • There's no fixed keyword in C++, perhaps in C#
  • The code you posted is perfectly valid in VS2012 Ultimate (and probably also Express)

From the above I might conclude you mismatched project and are trying to compile a C++ code in a C# environment.

Another reason that makes me think the above is the following error you get in a C# project if you try to compile the snippet above:

error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.

which refers exactly to the fixed keyword you were trying to use.


Short story: you're trying to compile a C++ code in a C# project. Paste that code in a C++ project, not a C# one. Those are two different languages.

Upvotes: 3

Related Questions