【问题标题】:C++ Error C3078 cannot 'new' an array of unknown boundsC++ 错误 C3078 无法“新建”未知边界数组
【发布时间】:2015-07-04 04:13:04
【问题描述】:

您好,我是编程新手,正在努力使用这些应用程序。这是错误代码:

错误 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;
}

【问题讨论】:

  • 这段代码有多个严重的问题。 getArray 写入一个野指针,并返回一个 int。 getArray 中的 cmets 是错误的(他们说了什么,但代码实际上做了其他事情)。 selectionsortArray 错误地使用 arr,这甚至无法编译。您似乎缺少对指针是什么的基本理解——返回并查看您的笔记或其他关于指针如何工作的内容。
  • 感谢您的帮助。由于某种原因,我一直在为指针而苦苦挣扎,并且不太明白在什么情况下,为什么以及用什么来取消引用和引用。0 我一直在回头看,但似乎并没有坚持下去。使用 get array 中的评论只是不好的做法。我让代码做一件事并决定更改它并且从未更改过 cmets .....也许找到视频或在线阅读会有所帮助。再次感谢您的宝贵时间!

标签: c++


【解决方案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;

【讨论】:

  • 它不需要是一个常量整数(在这种情况下意味着“在编译时已知”)。
【解决方案2】:

你的错误在这里:

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,否则会导致内存泄漏。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-27
    • 2018-05-30
    • 1970-01-01
    • 2018-07-28
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    相关资源
    最近更新 更多