当值不能恒定时如何修复"Expression must have a constant value"?
How to fix "Expression must have a constant value" when the value cannot be constant?
问题是我的错误输出不断说明"表达式必须具有恒定值"四次。
我已经使用了此站点,并尝试了分配内存并删除它,但对此一无所获。我更改了" mystr.length((;"到一个数字,虽然允许该程序运行,但它提供了无效的结果。
char letter = 'Y';
int position = 0;
string productNames[7] =
{ "Pen", "Paper", "Computer", "Pepsi", "Coke", "Book", "Scanner" };
int prizeOfproducts[7] = { 10, 2, 5000, 50, 45, 400, 2000 };
while (letter == 'Y')
{
string mystr = "";
cout << "enter the product name you want to search : ";
cin >> mystr;
int n = mystr.length();
char char_array[n + 1];
strcpy(char_array, mystr.c_str());
bool flag = false;
for (int i = 0; i < 7; i++)
{
int m = productNames[i].length();
char char_arrayOrig[m + 1];
strcpy(char_arrayOrig, productNames[i].c_str());
if (strstr(char_arrayOrig, char_array) == NULL)
{
flag = false;
}
else
{
flag = true;
position = i;
break;
}
}
if (!flag)
{
cout <<
"entered product not found in the product list . Do you want to search again Y/N ? : ";
cin >> letter;
}
我只是希望该程序工作而不是由于错误而停止,但是,我希望结果准确。
无需混合C - 样式std :: String和C-Style Char Arrays。如果要使用动态大小字符串-STD :: String是您所需的。如果您尝试使用char [],并且不知道它的大小 - 您可能不需要C ...
尝试考虑使用char []的地方的std :: string或std ::向量。这是您问题的直接解决方案的示例:
//char char_array[n + 1];
std::vector<char> char_array(n + 1);
相关文章:
- 为什么static_assert错误:即使我传递常量"expression must have a constant value"?
- 错误:"Left of getValue must have class/struct/union"
- unordered_set .find 方法无法编译的 C++ "expression must have class type"错误
- 编译为 cuda 内核调用提供了"expression must have integral or unscoped enum type"
- 调用 list.size() 方法给出错误"expression must have class type"
- 为什么我会收到"Expression must have a class type"错误?(C++)
- C++ 中数组的"expression must have pointer-to-object type"是什么?
- 当值不能恒定时如何修复"Expression must have a constant value"?
- 仅当链接函数 C++ 时,才会发生"Expression must have class type error"
- 我可以忽略"Intellisense: (E0028) expression must have a constant value"吗?
- 得到错误"left of '.toString' must have class/struct/union",即使 toString 应该有一个合适的对象
- 字符串初始化失败:"expression must have integral or unscoped enum type"
- 错误 - "expression must have a class type" - c++
- "Expression must have class type"错误
- deque.push_front() 给出错误"expression must have class type"
- 使用 "this" 关键字时的"Error: expression must have a pointer type"
- 使用可变宏时获得"Error: #28: expression must have a constant value "
- 在 2D 阵列分配期间对 calloc 进行"expression must have (pointer-to) function type"
- 当整数变量用于在 c++ 中声明数组大小时,错误显示为"Expression must have a const value"
- 错误消息在 C++ 中显示为"expression must have integral or enum type"