Variable length array of non-POD element type 'string' (aka 'basic_string') c++

后端 未结 3 2028
傲寒
傲寒 2021-01-07 10:16

I get this error in my c++ code Variable length array of non-POD element type string (aka basic_string).

string words[n         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-07 11:07

    Sorry, you need to write gcc --version to get the version.

    As others state, you shouldn't use variable-length arrays, but GCC does support them as an extension in C++. My GCC 4.4.4 compiles just fine with the following code:

    #include 
    #include 
    using namespace std;
    
    int main() {
      int n;
      cin >> n;
      string s[n];
      return 0;
    }
    

    Does that code compile for you? If it does, then you need to give us the smallest piece of code that fails.

    The best solution, though, is to use vector.

提交回复
热议问题