First, I write this code at vscode, it can be executed. Then I copy to visual studio, showing this error.
#include <Windows.h>
#include <string>
#include <cstdio>
using namespace std;
int main() {
string s = "abcdefg";
int arr[s.size()];
}
First, I write this code at vscode, it can be executed. Then I copy to visual studio, showing this error.
#include <Windows.h>
#include <string>
#include <cstdio>
using namespace std;
int main() {
string s = "abcdefg";
int arr[s.size()];
}
In C++ the size of array must be a compile time constant(constant expression)
. So the following is incorrect:
int n = 10;
int arr[n]; //incorrect
Correct way to write the above is:
const int n = 10;
int arr[n]; //correct
So the error
Expression must have constant value
indicates this error since pt_size
must be a constant expression(integral).
From cpp
The elements field within square brackets [], representing the number of elements in the array, must be a constant expression, since arrays are blocks of static memory whose size must be determined at compile time, before the program runs.