[英]c++ type error message from compiler, what does it mean?
我在fedora linux 13上使用g ++。我只是在c ++教科書中練習一些練習,而無法編譯該程序。 這是代碼:
double *MovieData::calcMed() {
double medianValue;
double *medValPtr = &medianValue;
*medValPtr = (sortArray[numStudents-1] / 2);
return medValPtr;
}
這是類聲明:
class MovieData
{
private:
int *students; // students points to int, will be dynamically allocated an array of integers.
int **sortArray; // A pointer that is pointing to an array of pointers.
double average; // Average movies seen by students.
double *median; // Median value of movies seen by students.
int *mode; // Mode value, or most frequent number of movies seen by students.
int numStudents; // Number of students in sample.
int totalMovies; // Total number of movies seen by all students in the sample.
double calcAvg(); // Method which calculates the average number of movies seen.
double *calcMed(); // Method that calculates the mean value of data.
int *calcMode(); // Method that calculates the mode of the data.
int calcTotalMovies(); // Method that calculates the total amount of movies seen.
void selectSort(); // Sort the Data using selection sort algorithm.
public:
MovieData(int num, int movies[]); // constructor
~MovieData(); // destructor
double getAvg() { return average; } // returns the average
double *getMed() { return median; } // returns the mean
int *getMode() { return mode; } // returns the mode
int getNumStudents() { return numStudents; } // returns the number of students in sample
};
這是我的構造函數和析構函數以及selectSort():
MovieData::MovieData(int num, int movies[]) {
numStudents = num;
// Now I will allocate memory for student and sortArray:
if(num > 0) {
students = new int[num];
sortArray = new int*[num];
// The arrays will now be initialized:
for(int index = 0;index < numStudents;index++) {
students[index] = movies[index];
sortArray[index] = &students[index];
}
selectSort(); // sort the elements of sortArray[] that point to the elements of students.
totalMovies = calcTotalMovies();
average = calcAvg();
median = calcMed();
mode = calcMode();
}
}
// Destructor:
// Delete the memory allocated in the constructor.
MovieData::~MovieData() {
if(numStudents > 0) {
delete [] students;
students = 0;
delete [] sortArray;
sortArray = 0;
}
}
// selectSort()
// performs selection sort algorithm on sortArray[],
// an array of pointers. Sorted on the values its
// elements point to.
void MovieData::selectSort() {
int scan, minIndex;
int *minElement;
for(scan = 0;scan < (numStudents - 1);scan++) {
minIndex = scan;
minElement = sortArray[scan];
for(int index = 0;index < numStudents;index++) {
if(*(sortArray[index]) < *minElement) {
minElement = sortArray[index];
minIndex = index;
}
}
sortArray[minIndex] = sortArray[scan];
sortArray[scan] = minElement;
}
}
編譯器給出此錯誤:
moviedata.cpp:在成員函數'double * MovieData :: calcMed()'中:
moviedata.cpp:82:錯誤:類型為'int *'和'double'的無效操作數為二進制'operator /'
我不確定該錯誤的原因,我試過靜態轉換類型而沒有運氣,此錯誤消息是什么意思?
您正在嘗試將指針除以雙精度數,而編譯器則說它不知道該怎么做。
sortArray可能由定義
int ** sortArray;
同樣值得注意的是,您正在返回指向堆棧變量的指針,一旦您退出該函數,該變量的值將是未定義的。
sortArray[numStudents - 1]
是一個指向int的指針,該指針不能位於除法的左側(當您記住指針是地址時,這很有意義)。 如果您發布更多代碼,我們可以幫助您更正它。
也許您想要這樣的東西:
int *MovieData::calcMed() {
return sortArray[(numStudents - 1) / 2];
}
這將返回數組中的中間元素,該元素應該是指向中間學生的指針。 我不清楚您為什么要對指針列表(而不是實際值)進行排序,或者為什么要在此處返回指針。 返回值+ 1將指示students
的下一個值,而不是數字上的下一個更大的值。 因此,您不妨返回實際的學生(來自students
int)。 如果執行此操作,則還可以在計數為偶數時平均兩個中間元素(此規則是典型中值算法的一部分)。
請注意,我將返回類型更改為int *
,即sortArray元素的類型。 另外,您的評論不正確。 這是中位數 ,而不是平均值 。
另外,您的選擇排序是錯誤的。 內部循環應從scan + 1
開始。
您的代碼顯示缺乏對指針的理解。 您需要做更多的閱讀和練習一些簡單的例子。
進一步來說:
double medianValue;
創建一個雙精度變量。 做什么的? 您顯然要返回double *
並返回指向局部變量的指針總是錯誤的 ,因為局部變量在函數結束時會被“回收”。
double *medValPtr = &medianValue;
創建一個名為medValPtr的指針,並將其設置為中值的位置。 好。
由於medValPtr的當前內容, *medValPtr = (sortArray[numStudents-1] / 2);
與鍵入medianValue = (sortArray[numStudents-1] / 2);
具有相同的效果medianValue = (sortArray[numStudents-1] / 2);
(假設它完全可以編譯)。
並不是因為sortArray[numStudents-1]
只是數組sortArray
的最后一項,而恰好是指向其他內容的指針。 您不能分割指針(通常可以這樣做,但是C ++不允許這樣做總是錯誤的 )。
最后,您return medValPtr;
這是錯誤的,因為medValPtr
指向局部變量。
您可能想要類似的東西:
int *MovieData::calcMed() {
return sortArray[numStudents/2];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.