gpt4 book ai didi

来自编译器的c++类型错误消息,这是什么意思?

转载 作者:太空宇宙 更新时间:2023-11-03 10:36:07 24 4
gpt4 key购买 nike

我在 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: In memberfunction 'double * MovieData::calcMed()':

moviedata.cpp:82: error: invalid operands of types 'int*' and 'double' to binary 'operator/'

我不确定这个错误是怎么回事,我试过静态转换类型但没有成功,这个错误消息是什么意思?

最佳答案

你正试图将一个指针除以一个 double ,编译器说它不知道该怎么做。

sortArray 可能定义为

int ** 排序数组;

同样值得注意的是,您将返回一个指向堆栈变量的指针,一旦您从函数中返回,该变量的值将是未定义的。

关于来自编译器的c++类型错误消息,这是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3099135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com