【问题标题】:Error: invalid conversion from 'int' to 'int*' [-fpermissive]错误:从“int”到“int*”的无效转换 [-fpermissive]
【发布时间】:2015-05-23 18:47:38
【问题描述】:

编译器错误:[错误] 从 '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 但您将它传递给应该提供数组的函数!因此给出了错误,我认为您以错误的顺序给出参数

标签: c++ arrays pointers


【解决方案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,而不是函数 printModeValues 为其第三个参数所预期的 int *

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

【讨论】:

    【解决方案2】:

    modeFrequency 正在返回 int

    int modeFrequency(const int * scores, size_t size)
    

    但您的 printModeValues 需要一个整数数组。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多