简体   繁体   中英

argument of type “int” incompatible with parameter of type “int”

Very new to programming and was asked to find errors in a program code as tutorial. While trying to fix it, I kept getting the line " argument of type 'int' incompatible with parameter of type 'int' " for the line labeled passing individual elements. Haven't learn about pointers, and don't really understand how functions work either, so there might be errors elsewhere.

#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] << " " ;
}

You are passing numbers[i] which is one int value whereas your function parameter expects an int array.

Change function definition to just void functionA ( int num ) and you should be able to output the int element that you pass.

Hope this helps you see the difference between int and int [] .

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

This function takes an array (well, really a pointer ) of int s, not a single int . You should change the signature in the declaration and definition to this:

void functionA ( int num )

Also note that you declare main as void main() , but it needs to be declared as returning an int .

for ( i=0; i<10; i++ )
        functionA ( numbers[i] ) ;

Here, you're passing the i-th element in the numbers array to functionA. Numbers is an array of Integers, so numbers[i] is an int.

void functionA ( int num[] )

functionA expects an Integer Array as input. You are passing an Integer, so it fails.

I suspect your compiler error was not "int is incompatible with int", but "int is incompatible with int*". The * is important, as it designates a pointer.

Depending on what you were trying to do, you have to either change functionA to take an int, instead of an int[] (in which case it prints the number passed to it), or pass "numbers" instead of "numbers[i]" to it and change functionA to iterate over the array (with a for-loop, for example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM