9

Possible Duplicate:
Is there a way to initialize an array with non-constant variables? (C++)

I have the following code:

vector<vector<vec2>> vinciP;
    int myLines = -1;
    myLines = drawPolyLineFile("vinci.dat", vinciP);
    if (myLines > -1)
    {
        cout << "\n\nSUCCESS";
        vec2 vPoints[myLines];
        for (int i = 0; i < NumPoints; ++i)
        {
            vPoints[i] = vinciP[0][i];
        }
    }

I'm getting an error on the line 'vec2 vPoints[myLines];' that says expressions must have a constant value. I don't understand why I'm getting this error, any help?

Is it because myLines could be negative? idk.

Community
  • 1
  • 1
y3di
  • 673
  • 2
  • 8
  • 13

5 Answers5

13
vec2 vPoints[myLines];

Since myLines is not a const expression ((which means, it is not known at compile-time), so the above code declares a variable length array which is not allowed in C++. Only C99 has this feature. Your compiler might have this as an extension (but that is not Standard C++).

The solution to such commom problem is : use std::vector<T> as:

std::vector<vec2> vPoints(myLines);

It should work now.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • GCC has this available as an extension in c++ mode. It is surprising to me this feature hasn't managed its way into c++. – Captain Giraffe Sep 20 '11 at 15:58
  • @CaptainGiraffe: VLA were introduced in C99, C++ was based on C98, and by the time C99 introduced VLA, C++ already had vector, so they never felt a need of pressing for it. – Alok Save Sep 20 '11 at 16:00
  • @Als, I was not aware that "C++ was based on C98", I'm assuming you are referring to 0x, maybe I should create a separate question for this, but the existence of std::vector does not really convince me of the non-usefulness of VLAs. – Captain Giraffe Sep 20 '11 at 16:05
  • 1
    @CaptainGiraffe: Indeed the usefulness `VLA` can provide in C++ are manyfold but it seems there is too much work to get it done to get those to work in C++. [Here](http://stackoverflow.com/questions/1887097/variable-length-arrays-in-c/1887178#1887178) is a thread that discusses this. – Alok Save Sep 20 '11 at 16:11
  • @CaptainGiraffe: No need to create another topic: Someone has already created this. See this : http://stackoverflow.com/questions/7458857/why-doesnt-c-support-dynamic-arrays-on-the-stack – Nawaz Sep 20 '11 at 16:14
  • @Als, Very relevant thread, thanks. JS - litb to the rescue once again, no further ?'s on my part. I would make a few counter-arguments Nawas on your reply, but combined I'm good. – Captain Giraffe Sep 20 '11 at 16:17
  • C98 above is a typo. C++ was based on C89. C++11 has adopted some features of C99 but not VLAs. VLAs will probably never be in C++. Consider 'void foo(int (&arr)[10]);' In C++ the size of an array is part of its type and is used in type checking, and VLAs simply cannot work this way. – bames53 Sep 20 '11 at 16:18
  • @barnes Yes (C89 of course =) the links provided makes these non-C complexities quite clear. Thanks. – Captain Giraffe Sep 20 '11 at 16:21
5

Is it because myLines could be negative?
No, It is because myLines is not a compile time constant.

Explanation:

vec2 vPoints[myLines];

Creates an array of variable length, where myLines value will be determined at Run-time. Variable length arrays are not allowed in C++. It was a feature introduced in C99, and C++ Standard does not support it. Some C++ compilers support it as an extension though but it is nevertheless non standard conforming.

For C++ size of an array should be known at compile time and hence must be compile time constant. myLines is not a compile time constant and hence the error.

You should use a std::vector

Alok Save
  • 202,538
  • 53
  • 430
  • 533
1
vec2 vPoints[myLines];

Array size must be a compile time constant. myLines is not a compile time constant. Instead, allocate the memory using new or even better to use std::vector.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
1

C++ does not have variable-length arrays. The size of an array must be determined at compile-time. The value of myLines is only known at runtime, so this won't work.

To have arrays whose size is only known at runtime, use std::vector.

std::vector<vec2> vPoints(myLines);
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
1

You are getting that error because static arrays need a static (constant) size. Since the number of components in your vPoints is dynamic, consider using a dynamic array instead. Or better yet stick with vector.

K-ballo
  • 80,396
  • 20
  • 159
  • 169