C++ C++;错误C3078不能';新';未知边界的数组
嗨,我是编程新手,正在为这些应用程序而挣扎。以下是错误代码: 错误C3078无法“新建”具有未知边界的数组第17行 我正在努力理解和掌握指针的概念。任何帮助都非常感谢您查看我的代码C++ C++;错误C3078不能';新';未知边界的数组,c++,C++,嗨,我是编程新手,正在为这些应用程序而挣扎。以下是错误代码: 错误C3078无法“新建”具有未知边界的数组第17行 我正在努力理解和掌握指针的概念。任何帮助都非常感谢您查看我的代码 #include <iostream> #include <iomanip>; using namespace std; //function prototypes int getArray(int num); void selectionsortArray(int *[], int); d
#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;
}
#包括
#包括,;
使用名称空间std;
//功能原型
intgetarray(intnum);
void selectionsortary(int*[],int);
双findAverage(整数*分数,整数);
无效数据(int*[],int);
int main()
{
int*scores=newint[];//动态分配数组
双倍总计=0,//累加器
平均;//保存平均测试分数
int testScores,//保存用户将输入的测试分数量
count;//计数器变量
//请求用户希望输入的测试分数量
cout>testScores;
getArray(testScores);
selectionsortArray(分数和测试分数);
库特
上述行将失败,因为您没有指定初始化的数组长度。在使用动态分配的C++数组声明中,需要一个数组大小的常数整数。
如果您按照以下方式更改代码,或者用一个适合您需要的数字代替10,将解决您的问题
int *scores = new int[10]; // To dynamically allocate an array
由于您使用new operator分配内存,因此应在使用后按如下方式解除分配:
delete[] scores;
您的错误如下:
int *scores = new int[]; // To dynamically allocate an array
<>你不能在C++中这样做。如果你不知道数组的大小,你可以创建指针:
int *scores;
当您知道大小时,您将能够在堆上分配内存:
scores = new int[5]; // To dynamically allocate an array
关于静态和动态分配之间的区别,您可以在此处阅读:
完成后删除分数
,否则会导致内存泄漏。此代码有多个严重问题。getArray
写入一个通配符,并返回一个整数。getArray
中的注释错误(它们说了些什么,但代码实际上做了其他事情).selectionsortArray
错误地获取了arr
,这甚至无法编译。您似乎对指针是什么缺乏基本的理解--请返回并查看您的笔记或任何有关指针工作原理的内容。感谢您的帮助。我一直在为指针而挣扎,但不太明白在什么情况下mstance,你为什么要取消引用和引用什么。0我一直在回过头来,阅读似乎并不坚持。get数组中的注释是一种糟糕的做法。我让代码做了一件事,并决定更改它,但从未更改注释……也许在线查找视频或阅读会有所帮助。再次感谢您的tim它不需要是一个常量整数(在本文中,这意味着“在编译时已知”)。
scores = new int[5]; // To dynamically allocate an array