C ++错误C3078不能'新'一系列未知界限

时间:2015-07-04 04:13:04

标签: c++

嗨,我是编程和使用这些应用程序的新手。这是错误代码:

  

错误C3078你不能'新'一系列未知界限第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;
}

2 个答案:

答案 0 :(得分:1)

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

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

如果您将代码更改为以下代码,或者代替10代替您的代码,将解决您的问题。

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

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

delete[] scores;

答案 1 :(得分:1)

您的错误在这里:

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

你不能用C ++做到这一点。如果你不知道数组的大小,你可以创建指针:

int *scores;

当您知道大小时,您将能够在堆上分配内存:

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

关于静态和动态分配之间的区别,您可以在这里阅读:

Declaring array of int

完成后删除scores,否则会导致内存泄漏。