C++ forbids variable-size array

后端 未结 3 1889
面向向阳花
面向向阳花 2021-01-17 08:18

I am using some existing code that someone else has written, and I cannot get it to compile (limited C experience here but I am trying to learn!).

utilities.

3条回答
  •  一生所求
    2021-01-17 09:06

    The error is correct. VLA(variable size arrays) are forbidden in C++. This is a VLA:

    char filename1char[filenameLength];
    

    What you probably meant is this:

    char *filename1 = new char[filenameLength];
    

    Which is not a VLA, but an array of chars allocated on the heap. Note that you should delete this pointer using operator delete[]:

    delete[] filename1;
    

提交回复
热议问题