简体   繁体   English

创建数组时表达式必须有常量值错误 C++

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

Good day.再会。 Faced an error "Expression must have a constant value" when creating an array.创建数组时遇到错误“表达式必须具有常量值”。 Find this question c++ array - expression must have a constant value , but these tips didn't solve the problem.找到这个问题c++ array - expression must have a constant value ,但是这些提示并没有解决问题。 Please help and I'm sorry for such requests, I just started to learn C ++请帮忙,我很抱歉有这样的要求,我刚开始学习 C++

Code:代码:

#include <iostream>
#include <math.h>
#include <cstdlib>
#include <string>
#include <sstream>
#include <conio.h>
#include <random>

int main()
{
   int userInput = 0, sumEvenIndexElements = 0, sumElementsBeetwenZero = 0;
   bool checkZeroElements = false;
   std::cout << "Write array length:" << std::endl;
   std::cin >> userInput;
   const int lengthOfArray = userInput;
   int numbers [lengthOfArray];
   std::default_random_engine generator;
   std::uniform_int_distribution<int> distribution(-10, 10);

   for (int index = 0; index < lengthOfArray; index++)
   {
      numbers[index] = new int[distribution(generator)];
      std::cout << numbers << std::endl;

      if (index % 2 == 0)
      {
         sumEvenIndexElements += *numbers[index];
      }

   }

   std::cout << "Sum even index elements: " << sumEvenIndexElements << std::endl;

   for (int index = 0; index < lengthOfArray; index++)
   {
      numbers[index] = new int[distribution(generator)];

      if (numbers[index] == 0)
      {
         checkZeroElements = !checkZeroElements;
      }

      if (checkZeroElements)
      {
         sumElementsBeetwenZero += *numbers[index];
      }

   }

   if (checkZeroElements)
   {
      std::cout << "Sorry, array have less than two zero elements.";
   }
   else
   {
      std::cout << "Sum even index elements: " << sumEvenIndexElements << std::endl;
   }

}

lengthOfArray is a constant variable, it's value would not be chagned during runtime after initialization. lengthOfArray是一个常量变量,它的值初始化后的运行时不会改变。

But it's value - userInput is not a constant, it dependes on runtime user input.但它的价值 - userInput不是一个常数,它取决于运行时用户输入。 As pointed here正如这里指出的

A constant value is an explicit number or character such as 1 or 0.5 or 'c'.常量值是一个明确的数字或​​字符,例如 1 或 0.5 或“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, eg:您应该使用 std::vector 而不是数组,如 Paul Evans 的回答中所建议的,或者为lengthOfArray分配一个适当的常量值,这将在编译时已知,例如:

const int lengthOfArray = 10;

Your code:您的代码:

   std::cout << "Write array length:" << std::endl;
   std::cin >> userInput;
   const int lengthOfArray = userInput;
   int numbers [lengthOfArray];

won't work as per the compiler error you're getting.根据您收到的编译器错误将无法正常工作。

std::vector plays a lot nicer: std::vector表现要好得多:

   std::cout << "Write array length:" << std::endl;
   std::cin >> userInput;
   const int lengthOfArray = userInput;
   std::vector<int> numbers(lengthOfArray, 0);

will create a std::vector of int of length lengthOfArray all initialised to 0 .将创建一个长度为lengthOfArrayint std::vector全部初始化为0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM