![](/img/trans.png)
[英]error: invalid conversion from 'int (*)[6]' to 'int' [-fpermissive]|
[英]Error: invalid conversion from 'int' to 'int*' [-fpermissive]
编译器错误:[错误]从'int'到'int *'的无效转换[-fpermissive]有人可以帮助我,并告诉我为什么我的程序给我这个错误吗?
错误的代码:
cout<<"Mode score: "<<printModeValues(scorePtr,size,modeFrequency(scorePtr,size));
我的代码:Main:
#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;
}
}
函数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);
^^^^
这就是错误的原因。
您的程序中还有其他错误。 例如在此功能
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
而不是数组。 或者,您必须自己动态分配数组。
modeFrequency返回int
int modeFrequency(const int * scores, size_t size)
但是您的printModeValues需要一个整数数组。
void printModeValues(const int *, size_t, int[]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.