c++ - 错误 : invalid conversion from 'int' to 'int*' [-fpermissive]

标签 c++ arrays pointers

编译器错误:[错误] 从“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[]);

函数原型(prototype):

//**** 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 而不是数组。或者您必须自己动态分配数组。

关于c++ - 错误 : invalid conversion from 'int' to 'int*' [-fpermissive],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30416380/

相关文章:

c++ - 用于嵌入式脚本/文本处理引擎的 Python vs Lua

c++ - 是从动态分配的数组索引超出范围的元素有效

c - 取消引用指针会破坏它的值(value)?

c++ - 在 MATLAB 中打开包含混合数据的 .gz 文件

c++ - 如何: non-root qml frontend and root-privileged worker threads

c# - 为什么 C# 不提供 C++ 风格的 'friend' 关键字?

javascript - 从对象数组传递某些参数

python - 为什么 maxmin 分而治之的实现比其他 maxmin 算法慢?

c - 是否可以将指针从结构类型转换为扩展 C 中第一个结构类型的另一种结构类型?

c++ - unique_ptr 的 remove_pointer