[英]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.
在尝试修复它的过程中,我一直在为标记为传递单个元素的行提供“类型'int'的参数与'int'类型的参数不兼容”。 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. 您传递的
numbers[i]
是一个int
值,而您的函数参数需要一个int
数组。
Change function definition to just void functionA ( int num )
and you should be able to output the int
element that you pass. 将函数定义更改为
void functionA ( int num )
,您应该能够输出传递的int
元素。
Hope this helps you see the difference between int
and int []
. 希望这有助于您了解
int
和int []
之间的区别。
void functionA ( int num[] )
{
cout << num << " " ;
}
This function takes an array (well, really a pointer ) of int
s, not a single int
. 这个函数接受
int
的数组(嗯,实际上是指针 ),而不是单个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
. 另请注意,您将
main
声明为void main()
,但需要将其声明为返回int
。
for ( i=0; i<10; i++ )
functionA ( numbers[i] ) ;
Here, you're passing the i-th element in the numbers array to functionA. 在这里,您将数字数组中的第i个元素传递给functionA。 Numbers is an array of Integers, so numbers[i] is an int.
Numbers是一个整数数组,因此numbers [i]是一个int。
void functionA ( int num[] )
functionA expects an Integer Array as input. functionA需要一个Integer数组作为输入。 You are passing an Integer, so it fails.
您正在传递一个Integer,因此它失败了。
I suspect your compiler error was not "int is incompatible with int", but "int is incompatible with int*". 我怀疑你的编译器错误不是“int与int不兼容”,但“int与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). 根据你要做的事情,你必须改变functionA来取一个int,而不是int [](在这种情况下它打印传递给它的数字),或者传递“数字”而不是“数字[i] ]“to it并更改functionA以迭代数组(例如,使用for循环)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.