2015-02-11 103 views
0

所以即時通訊對C++來說仍然很新穎,並且現在已經在執行一段程序。我認爲我正在慢慢獲得它,但不斷收到錯誤「智能感知:'*'的操作數必須是指針。」在第36行第10列。我需要做些什麼來解決這個錯誤?我就要去其他的功能,因爲我完成了額外的函數聲明每一個難過使用指針調用函數時出現錯誤

// This program will take input from the user and calculate the 
// average, median, and mode of the number of movies students see in a month. 

#include <iostream> 
using namespace std; 

// Function prototypes 
double median(int *, int); 
int mode(int *, int); 
int *makeArray(int); 
void getMovieData(int *, int); 
void selectionSort(int[], int); 
double average(int *, int); 

// variables 
int surveyed; 



int main() 
{ 
    cout << "This program will give the average, median, and mode of the number of movies students see in a month" << endl; 
    cout << "How many students were surveyed?" << endl; 
    cin >> surveyed; 

    int *array = new int[surveyed]; 

    for (int i = 0; i < surveyed; ++i) 
    { 
     cout << "How many movies did student " << i + 1 << " see?" << endl; 
     cin >> array[i]; 
    } 



    median(*array[surveyed], surveyed); 

} 


double median(int *array[], int num) 
{ 
    if (num % 2 != 0) 
    { 
     int temp = ((num + 1)/2) - 1; 
     cout << "The median of the number of movies seen by the students is " << array[temp] << endl; 
    } 
    else 
    { 
     cout << "The median of the number of movies seen by the students is " << array[(num/2) - 1] << " and " << array[num/2] << endl; 
    } 

} 
+3

所以'array'是int'的'數組。在第36行中,你可以執行'* array [surveyed]'。這在調查的索引處訪問陣列數組(在這裏是錯誤的命名)。這給出了一個int,然後*嘗試去引用。 int是一個原始類型,它不是一個指針,所以不能被去引用。 – 2015-02-11 19:34:55

+0

喵,將'array [surveyed]'改爲'array'。 – WhozCraig 2015-02-11 19:37:35

+1

'所以即時通訊對C++來說還是個新鮮事物,並且已經做了一段時間的程序了'所以你說你找不到幾十萬個例子中的一個例子,向你展示傳遞一個數組的正確方法,如何正確聲明功能? – PaulMcKenzie 2015-02-11 19:37:39

回答

2

問題:

  1. 在下面的行中使用的表達*array[surveyed]

    median(*array[surveyed], surveyed); 
    

    是不正確的。 array[surveyed]是數組中的surveyed個元素。它不是一個指針。解引用它是沒有意義的。

  2. 聲明中使用的第一個參數median的類型與定義中使用的類型不同。該聲明似乎是正確的。實施更改爲:

    double median(int *array, int num) 
    
  3. 解決您撥打median的方式。取而代之的

    median(*array[surveyed], surveyed); 
    

    使用

    median(array, surveyed); 
    
+0

非常感謝你。也意識到我已經忘記了在函數結束時返回,所以添加好ol後返回0;我能夠讓它運行。有時間轉到下一個功能。 =) – exo316 2015-02-11 19:45:02