【问题标题】:error with "expression must have a constant value"“表达式必须具有恒定值”的错误
【发布时间】:2021-01-16 22:36:30
【问题描述】:

我是 C++ 的新手。我正在尝试制作一个可以找到平均值、最大值、最小值和中值的程序。我不确定我在代码中做错了什么,double scores[n]; 出现错误:

#include <iostream>
#include<iomanip>
#include "ArrayFunction.h"

using namespace std;

int main() 
{
    int n;

    double scores[n];

    cout << "Enter the number of your scores: ";
    cin >> n;
    // creat the input array
     
    // read the elements into the array 
    for (int i = 0; i < n; i++)
    {
        cout << "Enter " << n << " scores: ";
        cin >> scores[i];
    }
    
    // call the functions and display for the result

    cout << "Average of Score is: " << calcAverage(scores, n) << endl;
    cout << "Min     : " << calcMin(scores, n) << endl;
    cout << "Max     : " << calcMax(scores, n) << endl;
    cout << "Median  : " << calcMedian(scores, n) << endl;
}

ArrayFunction.h

template <typename T>
long double calcAverage(T* arr, int size) {
    long double total = 0;
    //Compute the sum
    for (int i = 0; i < size; i++)
        total = total + *(arr + i);
    //return the average 
    return total / size;
}

template <typename T>
T calcMax(T* arr, int size) {
    //Initilize the max
    T max = arr[0];
    //Find the max out of the remaining elements
    for (int i = 1; i < size; i++)
        if (max < arr[i])
            max = arr[i];
    //Return the max element
    return max;
}

template <typename T>
T calcMin(T* arr, int size) {
    //Initilize the min element     
    T min = arr[0];
    //Find the min out of the remaining elements
    for (int i = 1; i < size; i++)
        if (min > arr[i])
            min = arr[i];
    //Return the max element
    return min;
}

template <typename T>
long double calcMedian(T* arr, int size) {
    T temp;
    //Create a temp array
    T arr2[size];
    //Copy the array
    for (int i = 0;i < size;i++)
        arr2[i] = arr[i];
    //Sort the array
    for (int i = 0;i < size - 1;i++) {
        for (int j = i + 1;j < size;j++) {
            if (arr2[i] > arr2[j]) {
                temp = arr2[i];
                arr2[i] = arr[j];
                arr2[j] = temp;
            }
        }
    }
    //IF the size of array is even then we add the mid and min+1 elements
    //compute the average
    if (size % 2 == 0) {
        return (arr2[size / 2] + arr2[size / 2 + 1]) / 2.0;
    }
    //If the size is odd the we return the middle element
    return arr2[size / 2];
}

我尝试输入一个数字并得到另一个错误:

数组类型 T 大小不可分配

我搜索过,C++ 不支持变长数组,我应该改用std::vector

【问题讨论】:

  • c++ 不支持变长数组并告诉我改用向量 This is correct。这是一些good documentation on std::vector。简短版本是double scores[n]; 应该是std::vector&lt;double&gt; scores(n); 并且所有T* arrs 应该是std::vector&lt;T&gt; &amp; arr
  • 另外,由于std::vector知道它的大小,所以size参数可以丢弃。
  • 在你扔掉绒毛并减少到minimal reproducible example之后,问题通常更容易理解。 需要多少代码来重现您的错误消息(作为编译器报告的第一个错误)?
  • 即使使用 VLA,n 也应在使用前初始化/设置。

标签: c++ arrays windows math average


【解决方案1】:

您可以在编译时声明一个已知大小的数组!所以大小必须是恒定的!

int values[10];
int values2[] ={1,2,3};

constexpr int size = 10;
int values3[size];

const int size2 =5;
int values4[size2];

以上所有示例都是在编译时使用 const size 定义的!

对于运行时初始化,您可以使用指针!

int* values = new int[1]; 

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
相关资源
最近更新 更多