2015-05-23 80 views
0

编译器错误:[错误]从'int'无效转换为'int *'[-fpermissive] 有人可以帮助我,告诉我为什么我的程序给我这个错误?错误:无效从'int'转换为'int *'[-fpermissive]

故障代码:

cout<<"Mode score: "<<printModeValues(scorePtr,size,modeFrequency(scorePtr,size)); 

我的代码: 主营:

#include <iostream> 
#include <string> 
#include "processScores.h" 

int main() { 
    int * scorePtr; 
    string * namePtr, scoresFileName; 

    cout<<"Enter the file name: "; 
    cin>>scoresFileName; 

    unsigned size=getRecordsNumber(scoresFileName); 
    scorePtr = new int[size]; 
    namePtr = new string[size]; 

    readRecords(scorePtr,namePtr,scoresFileName); 
    sort(scorePtr,namePtr,size); 

    cout<<"The records in ascending order of surnames are: \n"; 
    cout<<"Name   Score\n"; 
    cout<<"---------------------"<<endl; 
    printScores(scorePtr,namePtr,size); 

    cout<<endl; 
    cout<<"Highest score: "<<highest(scorePtr,size)<<"\t"; 
    printFoundNames(scorePtr,namePtr,size,highest(scorePtr,size)); 
    cout<<"Lowest score: "<<lowest(scorePtr,size)<<"\t";  
    printFoundNames(scorePtr,namePtr,size,lowest(scorePtr,size)); 
    cout<<"Mean score: "<<mean(scorePtr,size)<<endl; 
    cout<<"Mode score: "<<printModeValues(scorePtr,size,modeFrequency(scorePtr,size)); 
    cout<<"Modal value occurrences is "<<modeFrequency(scorePtr,size)<<" time\n"<<endl; 
    cout<<"Median score: "<<median(scorePtr,size)<<endl; 
    delete [] scorePtr; 
    delete [] namePtr; 
} 

头文件:

void printModeValues(const int *, size_t, int[]); 

函数原型:

//**** MODE FREQUENCY **** 
int modeFrequency(const int * scores, size_t size) 
{ 
    int y[size] , modes[size];//Sets all arrays equal to 0 
    int i,j,k,m,a,cnt,count=0,max=0,no_mode=0,mode_cnt=0; 
    double num; 

    for(k=0; k<size; k++)//Loop to count an array from left to right 
    { 
     cnt=0; 
     num=scores[k];//Num will equal the value of array x[k] 

     for(i=k; i<size; i++)//Nested loop to search for a value equal to x[k] 
     { 
      if(num==scores[i]) 
       cnt++;//if a number is found that is equal to x[k] count will go up by one 

     } 

     y[k]=cnt;//The array y[k] is initialized the value of whatever count is after the nested loop 

     if(cnt>=2)//If cnt is greater or equal to two then there must be atleast one mode, so no_mode goes up by one 
     { 
      no_mode++; 
     } 
    } 

if(no_mode==0)//after the for loops have excuted and still no_mode hasn't been incremented, there mustn't be a mode 
{ 
    //Print there in no mode and return control to main 
    modes[1]=-1; 
    // return modes; 
} 
    for(j=0; j<size; j++) 
//A loop to find the highest number in the array 
    { 
     if(y[j]>max) 
     max=y[j]; 
    } 
for(m=0; m<size; m++)//This loop finds how many modes there are in the data set 
{ 
    //If the max is equal to y[m] then that is a mode and mode_cnt is incremeted by one 
    if(max==y[m]) 
     mode_cnt++; 
} 
//cout<<"This data set has "<<mode_cnt<<" mode(s)"<<endl;//Prints out how many modes there are 
    for(m=0; m<size; m++) 
    { 
     if(max==y[m])//If max is equal to y[m] then the same sub set of array x[] is the actual mode 
     { 

      cout<<"The value "<<scores[m]<<" appeared "<<y[m]<<" times in the data set\n"<<endl; 
      modes[count]=scores[m]; 
      count++; 
     } 
    } 
return *modes; 
} 
//===================================================================================== 
//**** PRINT MODE VALUE **** 
void printModeValues(const int *scores, size_t size, int *mostAppearance) 
{ 
    if (mostAppearance[0]== -1) 
    { 
     cout<<"-1 Modal value occurance is one time "<<endl; 
    } 
    else 
    { 
     for (int a=0 ; a< sizeof(mostAppearance); a++) 
     { 
      cout<<mostAppearance[a]<<" "; 
     } 
     cout<<endl; 
    } 
} 
+0

为什么只需要一个值就足够了? –

+0

'modeFrequency'返回int,但是你将它传递给一个应该给出数组的函数!因此给出了这个错误,我认为你给出了错误顺序的参数 – Sinapse

回答

1

功能printModeValues声明如下方式

void printModeValues(const int *, size_t, int[]); 

正如你看到它的第三个参数声明如下int[]被调整到int *

在这份声明中

cout<<"Mode score: "<<printModeValues(scorePtr,size,modeFrequency(scorePtr,size)); 

调用该函数像

printModeValues(scorePtr,size,modeFrequency(scorePtr,size)) 

它用作函数modeFrequency返回的值的第三个参数。

但是这个函数有返回类型int,而不是int *被函数printModeValues其第三个参数

int modeFrequency(const int * scores, size_t size); 
^^^^ 

这是错误的原因exprected。

您的程序还有其他错误。对于该功能例如

void printModeValues(const int *scores, size_t size, int *mostAppearance) 
{ 
    if (mostAppearance[0]== -1) 
    { 
     cout<<"-1 Modal value occurance is one time "<<endl; 
    } 
    else 
    { 
     for (int a=0 ; a< sizeof(mostAppearance); a++) 
     { 
      cout<<mostAppearance[a]<<" "; 
     } 
     cout<<endl; 
    } 
} 

在这个循环语句中的条件

 for (int a=0 ; a< sizeof(mostAppearance); a++) 

因为操作者sizeof(mostAppearance)产生指针本身(通常是4或8个字节)的大小是没有意义的。它与数组中第一个元素被这个指针指向的元素数目不同。

看来你是打算从功能modeFrequency那是你想声明像

int * modeFrequency(const int * scores, size_t size); 

函数返回一个指针,并打算在返回指向数组的第一个元素modes

int * modeFrequency(const int * scores, size_t size) 
{ 
    int y[size] , modes[size];// 

    //... 

    return modes; 
} 

但即使在这种情况下,该函数将无效,因为它返回指向函数的本地对象的指针,因为数组modes是本地数组。而且,C++标准不允许使用可变长度的数组。因此,此声明

int y[size] , modes[size];// 

不符合C++标准。

我会建议使用标准类std::vector而不是程序中的数组。或者你必须自己动态分配数组。

0

modeFrequency类型为int返回

int modeFrequency(const int * scores, size_t size) 

但你printModeValues期待一个整数数组。

void printModeValues(const int *, size_t, int[]); 
相关问题