這是我正在爲類進行的任務。長話短說,我試圖調用main中的allocate()
函數來執行。我不斷收到testArray
未在此範圍內聲明的錯誤。「我有點困惑,如果我的返回語句是通過我的函數調用的,而不是如何在範圍內調用?我希望這是有道理的,我很困惑我用這些指針和功能。任何建議表示讚賞,感謝。'testArray未在此範圍內聲明'
#include <iostream>
using namespace std;
int* allocate(int&);
int* allocate(int &numOfScores)
{
int *testArray;
//prompt user for scores
cout << "How many test scores would\n";
cout << "you like to process: " << endl;
cin >> numOfScores;
//dynammically allocate an arrray to hold the scores
testArray = new int[numOfScores];
//get the scores from user
for(int count = 0; count < numOfScores; count++)
{
cout << "Enter Score: " << endl;
cin >> testArray[count];
}
//release the memory that was allocated for *ptr
delete [] testArray;
testArray = 0;
return testArray;
}
int main()
{
allocate(testArray);
return 0;
}
我明白了!謝謝大家的建議,我想因爲我正在調用allocate()函數,它會返回而不管範圍,我現在明白了,謝謝。 – Gmenfan83 2012-03-20 23:40:38