該程序將編譯,但不會運行。它運行每當我收到這樣的:這編譯,但它不運行。有任何想法嗎?
「在0x00D761EE在Assignment3.1.exe未處理的異常:0000005:訪問衝突讀取位置00000000」
我在做什麼錯?
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
//Function Prototype
void getSize(int *);
float *getValues(int *);
float getMax(const float *, const int *);
int main()
{
float * ptData = nullptr;
int * ptr = nullptr;
int num;
getSize(&num);
float values = *getValues(&num);
float max = getMax(ptData, &num);
return 0;
}
//*******************************************************************************************************
void getSize(int * ptr)
{
cout << "Please enter a size to the array: ";
cin >> *ptr;
while (*ptr <= 1)
{
cout << "!!!Error: an array's size cannot be less than or equal to 1!\n";
cout << "Please enter a size to the array: ";
cin >> *ptr;
}
}
//*******************************************************************************************************
float * getValues(int * ptr)
{
float * ptData = new float[*ptr];
cout << "Please enter all values of the array: \n";
for (int count = 0; count < *ptr; count++)
{
cout << "Value " << (count + 1) << ": ";
cin >> ptData[count];
}
delete[] ptData;
ptData = 0;
return ptData;
}
//*******************************************************************************************************
float getMax(const float * ptData, const int * ptr)
{
float highest;
highest = *ptData;
for (int count = 1; count < *ptr; count++)
{
if (ptData[count] > highest)
highest = ptData[count];
}
return highest;
}
*我在做什麼錯?* - 其一,拒絕使用'的std :: VECTOR'代替指針。 – PaulMcKenzie
對於兩個,未能在調試器中單步查找問題並確定原因。地址0x00000000表示訪問一個nul指針。使用調試器可以幫助您識別哪些地方以及具體是哪些地方。如果您還沒有學會使用該調試器,現在是開始學習的最佳時機。 –
* Assignment3 * - 如果這真的是一個給你的C++課程的任務,請離開這個。沒有人用這種方式編寫C++程序。 – PaulMcKenzie