c++ - "int"类型的参数与 "int"类型的参数不兼容

标签 c++

编程新手,被要求在教程中查找程序代码中的错误。在尝试修复它时,我一直在为标记为传递单个元素的行获取行“'int'类型的参数与'int'类型的参数不兼容”。没有学过指针,也没有真正理解函数是如何工作的,所以可能其他地方有错误。

#include <iostream>
using namespace std;

void functionA ( int num[] ) ;
void functionB ( int newnumbers[] ) ;
void functionC ( int newnumbers[] ) ;

void main ()
{
    int numbers[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } ;
    int i;

    for ( i=0; i<10; i++ )
        functionA ( numbers[i] ) ;          // passing individual elements

    cout << "\n\n" ;
    functionB ( numbers ) ;                 // passing the whole array
    functionC ( numbers ) ;                 // passing the whole array

    cout << "\n\n" ;
}

void functionA ( int num[] )
{
    cout << num << " " ;
}

void functionB ( int newnumbers[] )
{
    for ( int i=0; i<10; i++ )
        newnumbers[i] = newnumbers[i] * 5 ;
}

void functionC ( int newnumbers[] )
{
    for ( int i=0; i<10; i++ )
        cout << newnumbers[i] << " " ;
}

最佳答案

您正在传递 numbers[i],这是一个 int 值,而您的函数参数需要一个 int 数组。

将函数定义更改为 void functionA ( int num ),您应该能够输出您传递的 int 元素。

希望这可以帮助您了解 intint [] 之间的区别。

关于c++ - "int"类型的参数与 "int"类型的参数不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43915621/

相关文章:

c++ - 试图解释 Windows 操作系统上的用户 session 状态

c++ - 我的代码在终端中没有正确执行

c++ - `*this` 可以是 `move()` d 吗?

c++ - QT 5.6 QWebEngine 不保存 cookie

c++ - 如何在运行时更改 styleSheet 属性?

c++ - 在 C++ 中链接用户转换

c# - C#中如何在类外定义构造函数

c++ - 对静态成员的 undefined reference

c++ - 如何使用 astyle 格式化 C++ 方法中的左大括号?

c++ - 如何在 vc 中停止正在进行的上传