调用函数时出错:“类型”int“的参数与类型”int *“的参数不兼容

时间:2012-11-14 20:40:20

标签: c++

我被分配了一个需要25个双打数组的程序。然后需要翻转并显示。我似乎无法使flipArray功能起作用。

#include <iostream>

using namespace std;

const int NUMSCORES = 25; 
//double getData(double &myArray);
void flipArray (int arr[]);


int main(void)
{   
    int scores[NUMSCORES], i;
    for(i=0; i<NUMSCORES; i++){
        cout << "Please enter scores #" << i+1 << ": ";
        cin >> scores[i];
    }

    cout << "Your test scores:\n";
    for(i=0; i<NUMSCORES; i++)
        cout << "\tTest score #" << i+1 << ": " << scores[i] << endl;

    flipArray(NUMSCORES);
    return;
    }

void flipArray(int arr[])
{
    int j;
    for (j=NUMSCORES-1; j>=0; j--)
        cout << arr[j] << "\t";
}

2 个答案:

答案 0 :(得分:1)

flipArray(NUMSCORES);

您的问题是您的参数是分数(int),而不是数组(将作为int*传递)。试试这个:

flipArray(scores);

答案 1 :(得分:1)

错误消息讲述了整个故事:

prog.cpp: In function ‘int main()’:
prog.cpp:22: error: invalid conversion from ‘int’ to ‘int*’
prog.cpp:22: error:   initializing argument 1 of ‘void flipArray(int*)’
prog.cpp:23: error: return-statement with no value, in function returning ‘int’

这两行是错误的:

flipArray(NUMSCORES);
return;

在第一行中,当你应该传入数组本身时,你传入一个int,即数组的大小。

在第二行中,您未能从main指定返回值。

尝试:

flipArray(scores);
return 0;