-1

Initialize const int sz = copy_wav_v;to set my size of name_wav[sz] but its keep error

error C2057: expected constant expression

//some code
                    if(char_chck >= 7 && character != 13)
                    {
                        copy_wav[copy_wav_v] = Readfile[i];
                        copy_wav_v++;
                    //  wav_n->Text=System::Convert::ToString(copy_wav_v);

                    }
                    if(character == 13)
                    {

                    //  const int sz = System::Convert::ToInt32(wav_n->Text);
                        const int sz = copy_wav_v;
                        char name_wav[sz]; //error starts here
                        for(int j = 0; j<=copy_wav_v;j++)
                        {
                            name_wav[j] = copy_wav[j];
                            if(j==copy_wav_v)
                            {
                                wav_name->Text= gcnew String(name_wav);
                            }
                        }
                    }
                    //some code

I am currently making a Soundboard System with Windows Form Application.

  • What is the question? copy_wav_v might not be assigned a value –  Apr 07 '19 at 14:55
  • "I keep do trial and errors" - that's the *wrong* way to learn C++. Read [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead. – Jesper Juhl Apr 07 '19 at 14:56
  • Your question is missing a question. *error starts here* is not enough to tell us what the problem is. – super Apr 07 '19 at 15:01
  • Please post a [mcve] – sjsam Apr 07 '19 at 15:04
  • 2
    First of all, the language you're using is not C++ but C++/CLI, which is a different language. Apart from that, I would highly recommend against using C++/CLI for building Windows Forms applications. If you want to build a Windows Forms application, use C# or any other proper .NET language to do so. C++/CLI is not meant to be used for building Windows Forms applications. It's meant to be used for writing interop layers that sit between .NET and native code. Especially if you don't already have a lot of experience with C++ and .NET, C++/CLI is definitely not what you should be using… – Michael Kenzel Apr 07 '19 at 15:04
  • the question is updated with more detailed to understand – Jhonrey Daffon Apr 07 '19 at 15:06
  • @peakpeak `copy_wav_v` means the length of a string – Jhonrey Daffon Apr 07 '19 at 15:09
  • 1
    What the compiler wants is a *compile-time* constant expression, not a `const` variable with a value only known at execution time. – molbdnilo Apr 07 '19 at 15:13
  • *char name_wav[sz]; //error starts here* Yes, as molbdnilo says, you cannot define an array at run-time. Use vector for this. –  Apr 07 '19 at 15:24
  • C style array is for fixed size, known at compile time. C++ `std::vector` are for dynamically sized at run time. – Eljay Apr 07 '19 at 15:42

1 Answers1

2

You are declaring a static array. Statically declared arrays are allocated memory at compile time and their size is fixed, i.e., cannot be changed later.

Your sz value is defined at runtime.

You need to use a dynamic array. E.g. take a look at std::vector

johnny b
  • 822
  • 6
  • 9