【问题标题】:Dynamically allocated C++ array动态分配的 C++ 数组
【发布时间】:2014-08-19 22:20:31
【问题描述】:

所以下面是我的代码,由于某种原因它不会编译。我无法从允许用户动态分配一个数组,到使用指向该数组的指针数组,再到对该动态分配的数组进行排序。我不断收到编译器错误提示:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'arrPtrscores': unknown size

另外,我需要知道如何使用输入验证来确保没有输入负测试分数。

代码如下:

//Write a program that dynamically allocates an array large enough to hold
//a user-defined number of test scores. Once all the scores are entered,
//the array should be passed to a function that sorts them in ascending order.
//Another function should be called that calculates the average score. The
//program should display the sorted list of scores and averages with
//appropriate headings. Use pointer notation rather than array notation whenever
//possible.

//Input Validation: Do not accept negative numbers for test scores. 

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

//Function prototypes.
void arrSelectSort(int *, int);
void showArray(int *, int);
void showArrPtr(int *, int);

int main()
{
    int *scores, //To dynamically allocate an array (pointer)
    total = 0, //Accumulator
    average; //To hold average test scores. 

    int numTests, //To hold the number of days of tests
    count; //Counter variable

    //Get the number of tests.
    cout << "How many tests would you like to enter? ";
    cin >> numTests;

    //Dynamically allocate an array large enough to hold that many test scores.
    scores = new int[numTests];


    //Get the scores for each test. 
    cout << "\nEnter the test scores.\n";
    for (count = 0; count < numTests; count++)
    {
        cout << "Test " << (count + 1) << ":  ";
        cin >> scores[count];
    }

    //Calculate the total test score. 
    for (count = 0; count < numTests; count++)
    {
        total += scores[count];
    }


    //int *
    //Calculate the average sales per day.
    average = total / numTests;

    //Display the results.
    cout << "\n\nTotal Score:  " << total << endl;
    cout << "\n\nAverage Score:  " << average << endl;

    //Free dynamically allocated memory
    delete[] scores;
    scores = 0; //Make scores point to null.

    //An array of pointers to int.
    int *arrPtrscores[count];

    //Sort the elements of the array of pointers. 
    arrSelectSort(scores, scores[count]);

    //Display the Test Scores in ascending order.
    cout<<"In ascending order, the test scores are: \n";

    showArrPtr(scores, scores[count]);

    system("pause");

    return 0;
}

//Definition of function arrSelectSort. 
//This function performs an ascending order selection sort.
void arrSelectSort(int *arr[], int size)
{
    int startScan;
    int minIndex;
    int *minElem;

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

//Definition of function showArray. 
//This function displays the contents of arr. size is the number of 
//elements.

void showArray(const int arr[], int size)
{
    for(int count = 0; count < size; count++)
        cout<<arr[count]<< " ";
    cout<<endl;
}

//Definition of function showArrPtr.
//This function displays the contents of the array pointed to by arr.
//size is the number of elements.

void showArrPtr(int *arr[], int size)
{
    for(int count = 0; count < size; count++)
        cout<<*(arr[count])<<" ";
    cout<<endl;
}

【问题讨论】:

  • int **arrPtrscores = new int*[count];
  • int *arrPtrscores[count]; 您的问题很可能在这里。可变大小的堆栈分配数组是特定编译器的扩展(您的显然不支持)。正如@NeilKirk 建议的那样,在那里使用new[]。
  • 所以我会将该定义添加到 int main() 中?原谅基本的问题。我是一个绝对的初学者。非常感谢!
  • 对不起,我明白你在说什么。替换该行。谢谢。没有睡觉。我会使用 while 循环来进行输入验证吗?
  • 我尝试使用 int *arrPtrscores = new int*[count]; 编译它行,但它仍然不会编译。错误如下: 错误 C2057: 预期的常量表达式 C2466: 无法分配常量大小为 0 的数组 C2440: 'initializing': cannot convert from 'int **' to 'int **[]'

标签: c++ arrays pointers dynamic


【解决方案1】:
//Free dynamically allocated memory
delete[] scores;
scores = 0;               <- u delete all your scores

int *arrPtrscores[count];

arrSelectSort(scores, scores[count]); <- and pass null pointer to the function

然后你尝试从 arrSelectSort 函数中读取空指针

minElem = arr[startScan];

另外你可能想要这个arrSelectSort(scores, count)而不是arrSelectSort(scores, scores[count])

还可以在此行的分数前添加&amp;:

arrSelectSort(&scores, scores[count]);
showArrPtr(&scores, count);

【讨论】:

  • 感谢您的意见!注释掉删除分数行现在只给我留下一个错误。错误 C2466:无法分配常量大小为 0 的数组。实际上有 2 个错误。错误 C2133:“arrPtrscores”:未知大小。 @Quest
  • 再次阅读,非常仔细 Neil Kirks 评论:int **arrPtrscores = new int*[count];
  • 也许我理解错了?我替换了行 int *arrPtrscores[count]; with int **arrPtrscores = new int*[count];我仍然有错误。然后我尝试了 int *arrPtrscores = new int*[count];我再次收到该行的错误。我是不是太从字面上理解了这个提示?再次,绝对的初学者。谢谢! @Quest
猜你喜欢
  • 2010-10-02
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 1970-01-01
  • 2010-11-27
  • 2015-02-10
  • 2021-11-24
  • 2022-01-17
相关资源
最近更新 更多