C++ 错误 C3078 不能 'new' 未知边界数组

标签 c++

您好,我是编程新手,正在为这些应用程序苦苦挣扎。这是错误代码:

Error C3078 you cannot 'new' an array of unknown bounds line 17

我正在努力理解和掌握指针的概念。任何帮助都非常感谢您查看我的代码。

#include <iostream>
#include <iomanip>;
using namespace std;

//function prototypes
int getArray(int num);
void selectionsortArray(int *[], int);
double findAverage(int *scores, int nums);
void showSortedArray(int *[], int);

int main()
{
int *scores = new int[]; // To dynamically allocate an array

double total = 0, //accumulator
        average; //to hold average test scores

int testScores, // To hold the amount of test scores the user will enter
    count; //Counter variable

// Request the amount of test scores the user would like to enter
cout << "How many test scores do you wish to process: ";
cin >> testScores;

getArray(testScores);

selectionsortArray(&scores, testScores);

cout << "Test scores sorted:\n\n";
showSortedArray(&scores, testScores);

average = findAverage(scores, testScores);

//set precision
cout << setprecision(2) << fixed;
cout << "\tAverage Score\n";
cout << average;






}

int getArray(int num)
{
int *array, ptr; //set array pointer equal to 0
int count;

cout << "\tPlease enter Test Scores by percent:\n";
for (count = 0; count < num; count++)
{
    cout << "Test score #" << (count+1)<< ": ";
    cin >> array[count];
}

ptr = *array; //Set the ptr to be returned with the array

return ptr; // return ptr
}

void selectionsortArray(int *arr[], int testScores)
{
int startscan, minIndex;
int *minElem;

for (startscan = 0; startscan < (testScores - 1); startscan++) 
{
    minIndex = startscan;
    minElem = arr[startscan];
    for(int index = startscan + 1; index < testScores; index++)
    {
        if (*(arr[index]) < *minElem)
        {
            minElem = arr[index];
            minIndex = index;
        }
    }
    arr[minIndex] = arr[startscan];
    arr[startscan] = minElem;
}
}

void showSortedArray(int *arr[], int testScores)
{
for ( int count = 0; count < testScores; count ++)
    cout << *(arr[count]) << " \n";
}

double findAverage(int *scores, int testScores)
{
double average = 0;

for (int count = 0; count < testScores; count++)
    average += scores[count];

average /= testScores;

return average;
}

最佳答案

int *scores = new int[]; // To dynamically allocate an array

上一行将失败并显示您指定的错误消息,因为您没有指定初始化的数组长度。在使用动态分配的 C++ 数组声明中,需要一个常量整数作为数组大小。

如果您按如下方式更改代码,或者用适合您需要的数字代替 10 ,将解决您的问题。

int *scores = new int[10]; // To dynamically allocate an array

由于您使用 new 运算符分配内存,因此您应该在使用后按如下方式解除分配:

delete[] scores;

关于C++ 错误 C3078 不能 'new' 未知边界数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31216803/

相关文章:

c++ - Qt qglobal.h 的 Boost 版本

c++ - 未定义、未指定和实现定义的行为

C++ 模板类型在 typedef 工作的地方中断

c++ - 使用模板的嵌套类中的未知类型名称 'class'

c++ - 使用 _GLIBCXX_CXX11_ABI 来使用具有 C++ 11/14 功能的 pre-5.1 C++ ABI 有什么影响?

c++ - 我如何学习 Visual C++?

c++ - 为什么来自 boost::smart_ptr 的 &this_type::px 返回 1?

c++ - 多进程同步的输出流锁定?

c++ - 从函数返回 "variable string literal"

c++ - 在 C++ 中调用初始化列表中的函数