编译错误:[错误]来自' 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 :(得分: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);
^^^^
这是错误的原因。
您的计划中还有其他错误。例如,在这个函数中
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
而不是数组。或者您必须自己动态分配数组。
答案 1 :(得分:0)
modeFrequency正在返回int
<table border="1" class="table table-striped">
<tr>
<th>Commits</th>
<th>Comments</th>
<th>Parent
<br>Branch(es)</th>
<th>Date</th>
<th>Username</th>
</tr>
<tr ng-repeat="commit in commitsArray.values">
<td>{{commit.hash.substr(0,6)}}</td>
<td>{{commit.message}}</td>
<td>
<p ng-repeat="parent in commit.parents">
{{parent.hash.substr(0,6)}}
</p>
</td>
<td>{{commit.date}}</td>
<td>{{commit.author.raw}}</td>
</tr>
</table>
Page {{commitsArray.page}}, {{commitsArray.pagelen}} commits.
<a ng-src="{{commitsArray.next}}">Next Page</a>
但是你的printModeValues需要一个整数数组。
int modeFrequency(const int * scores, size_t size)