Expression must have a constant value error when creating an array C++

huangapple go评论119阅读模式
英文:

Expression must have a constant value error when creating an array C++

问题

抱歉,你的代码中存在一些问题,我会帮你指出并提供一些修复建议。以下是翻译好的部分:

  1. Good day. Faced an error "Expression must have a constant value" when creating an array. Find this question <https://stackoverflow.com/questions/9219712/c-array-expression-must-have-a constant-value#>, but these tips didn't solve the problem. Please help and I'm sorry for such requests, I just started to learn C ++
  2. Code:
  3. #include <iostream>
  4. #include <math.h>
  5. #include <cstdlib>
  6. #include <string>
  7. #include <sstream>
  8. #include <conio.h>
  9. #include <random>
  10. int main()
  11. {
  12. int userInput = 0, sumEvenIndexElements = 0, sumElementsBetweenZero = 0;
  13. bool checkZeroElements = false;
  14. std::cout << "Write array length:" << std::endl;
  15. std::cin >> userInput;
  16. const int lengthOfArray = userInput;
  17. int *numbers = new int[lengthOfArray]; // 使用new分配动态数组
  18. std::default_random_engine generator;
  19. std::uniform_int_distribution<int> distribution(-10, 10);
  20. for (int index = 0; index < lengthOfArray; index++)
  21. {
  22. numbers[index] = distribution(generator); // 存储随机数而不是分配新的int数组
  23. std::cout << numbers[index] << std::endl;
  24. if (index % 2 == 0)
  25. {
  26. sumEvenIndexElements += numbers[index]; // 累加偶数索引元素
  27. }
  28. }
  29. std::cout << "Sum even index elements: " << sumEvenIndexElements << std::endl;
  30. for (int index = 0; index < lengthOfArray; index++)
  31. {
  32. if (numbers[index] == 0)
  33. {
  34. checkZeroElements = !checkZeroElements;
  35. }
  36. if (checkZeroElements)
  37. {
  38. sumElementsBetweenZero += numbers[index]; // 累加两个零之间的元素
  39. }
  40. }
  41. if (!checkZeroElements)
  42. {
  43. std::cout << "Sorry, array has less than two zero elements.";
  44. }
  45. else
  46. {
  47. std::cout << "Sum elements between two zeros: " << sumElementsBetweenZero << std::endl;
  48. }
  49. delete[] numbers; // 释放动态分配的数组内存
  50. return 0;
  51. }

请注意,我已经做了一些修改来修复代码中的错误。希望这对你有所帮助。如果你有任何其他问题,请随时提问。

英文:

Good day. Faced an error "Expression must have a constant value" when creating an array. Find this question <https://stackoverflow.com/questions/9219712/c-array-expression-must-have-a-constant-value#>, but these tips didn’t solve the problem. Please help and I'm sorry for such requests, I just started to learn C ++

Code:

  1. #include &lt;iostream&gt;
  2. #include &lt;math.h&gt;
  3. #include &lt;cstdlib&gt;
  4. #include &lt;string&gt;
  5. #include &lt;sstream&gt;
  6. #include &lt;conio.h&gt;
  7. #include &lt;random&gt;
  8. int main()
  9. {
  10. int userInput = 0, sumEvenIndexElements = 0, sumElementsBeetwenZero = 0;
  11. bool checkZeroElements = false;
  12. std::cout &lt;&lt; &quot;Write array length:&quot; &lt;&lt; std::endl;
  13. std::cin &gt;&gt; userInput;
  14. const int lengthOfArray = userInput;
  15. int numbers [lengthOfArray];
  16. std::default_random_engine generator;
  17. std::uniform_int_distribution&lt;int&gt; distribution(-10, 10);
  18. for (int index = 0; index &lt; lengthOfArray; index++)
  19. {
  20. numbers[index] = new int[distribution(generator)];
  21. std::cout &lt;&lt; numbers &lt;&lt; std::endl;
  22. if (index % 2 == 0)
  23. {
  24. sumEvenIndexElements += *numbers[index];
  25. }
  26. }
  27. std::cout &lt;&lt; &quot;Sum even index elements: &quot; &lt;&lt; sumEvenIndexElements &lt;&lt; std::endl;
  28. for (int index = 0; index &lt; lengthOfArray; index++)
  29. {
  30. numbers[index] = new int[distribution(generator)];
  31. if (numbers[index] == 0)
  32. {
  33. checkZeroElements = !checkZeroElements;
  34. }
  35. if (checkZeroElements)
  36. {
  37. sumElementsBeetwenZero += *numbers[index];
  38. }
  39. }
  40. if (checkZeroElements)
  41. {
  42. std::cout &lt;&lt; &quot;Sorry, array have less than two zero elements.&quot;;
  43. }
  44. else
  45. {
  46. std::cout &lt;&lt; &quot;Sum even index elements: &quot; &lt;&lt; sumEvenIndexElements &lt;&lt; std::endl;
  47. }
  48. }

答案1

得分: 1

lengthOfArray 是一个常量变量,它的值在初始化之后不会在运行时改变。

但它的值 - userInput 不是一个常量,它依赖于运行时用户的输入。正如在这里所指出的

常量值 是一个明确的数字或字符,例如1或0.5或'c'。

建议您使用std::vector而不是数组,如Paul Evans的答案中所建议的,或者为lengthOfArray分配一个适当的常量值,该值将在编译时已知,例如:

  1. const int lengthOfArray = 10;
英文:

lengthOfArray is a constant variable, it's value would not be chagned during runtime after initialization.

But it's value - userInput is not a constant, it dependes on runtime user input. As pointed here

> A constant value is an explicit number or character such as 1 or 0.5
> or ‘c’.

You should use std::vector instead of an array, as suggested in Paul Evans's answer, or assign a proper constant value to the lengthOfArray , which will be known at the time of compilation, e.g.:

  1. const int lengthOfArray = 10;

答案2

得分: 0

Your code:

  1. std::cout << "写入数组长度:" << std::endl;
  2. std::cin >> userInput;
  3. const int lengthOfArray = userInput;
  4. int numbers[lengthOfArray];

无法编译成功,根据你所遇到的编译器错误。

std::vector 更容易使用:

  1. std::cout << "写入数组长度:" << std::endl;
  2. std::cin >> userInput;
  3. const int lengthOfArray = userInput;
  4. std::vector<int> numbers(lengthOfArray, 0);

将创建一个长度为lengthOfArraystd::vector,并且所有元素都初始化为0

英文:

Your code:

  1. std::cout &lt;&lt; &quot;Write array length:&quot; &lt;&lt; std::endl;
  2. std::cin &gt;&gt; userInput;
  3. const int lengthOfArray = userInput;
  4. int numbers [lengthOfArray];

won't work as per the compiler error you're getting.

std::vector plays a lot nicer:

  1. std::cout &lt;&lt; &quot;Write array length:&quot; &lt;&lt; std::endl;
  2. std::cin &gt;&gt; userInput;
  3. const int lengthOfArray = userInput;
  4. std::vector&lt;int&gt; numbers(lengthOfArray, 0);

will create a std::vector of int of length lengthOfArray all initialised to 0.

huangapple
  • 本文由 发表于 2020年1月3日 18:34:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定