编译器错误:[错误]从'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;
}
}
为什么只需要一个值就足够了? –
'modeFrequency'返回int,但是你将它传递给一个应该给出数组的函数!因此给出了这个错误,我认为你给出了错误顺序的参数 – Sinapse