g++编译器无法识别我的函数
g++ compiler is not recognizing my function
我刚刚开始编写代码,现在正在学习数组。我正在尝试编写一个程序,该程序接收数组列表,并告诉第一个或最后一个数字是2。为此,我使用了一个函数。
我的代码看起来像:
#include <iostream>
using namespace std;
const int size = 6;
bool firstlast(int array[size]);
int main()
{
int array[size];
for (int index = 0; index < size; index++)
{
cout << "Enter value for array[" << index << "]n";
cin >> array[index];
}
bool check = firstlast(array[size]);
if (check)
cout << "The array either starts or ends in 2!n";
else
cout << "The array does not start or end with 2.n";
return 0;
}
bool firstlast(int array[size])
{
if (array[0] == 2)
return true;
if (array[size - 1] == 2)
return true;
return false;
}
我做错了什么?编译器给了我错误:
candidate function not viable: no known conversion from 'int' to 'int *' for 1st argument; take the address of the argument with and
编译器可以很好地识别您的函数。
问题在于您的代码调用函数的方式
bool check = firstlast(array[size]);
其试图将CCD_ 1(array
的不存在的元素(传递给期望指针的函数。
电话,大概应该是
bool check = firstlast(array);
因为数组在传递给函数时被隐式地转换为指针。
此代码为
bool check = firstlast(array[size], size);
尝试传递数组的第CCD_ 3个元素,而不是数组本身。在C++中,数组是通过指针传递的,即使使用数组语法编写函数参数也是如此。
为了避免混淆自己,请将firstlast
更改为
bool firstlast`(int* array, int size)`
并用称之为
bool check = firstlast(array, size);
相关文章:
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- 在哪里放置我的函数?进入我的母语 Gui 还是进入我的演示者?
- 查找定义我的 C/C++ 函数/宏的文件比'grep'更简单的方法
- 为什么我的 constexpr 对象在我的函数中不是 constexpr?
- 为什么我的函数接受"std::string"进行排序不会改变它?
- 为什么 emscripten 不编译我的函数?
- binary_search() 函数在我的函数体中不起作用
- 如何将不同大小的数组传递给我的 C++ 函数?
- 如果我想使用 cout 为我的函数提供任何输入......我该如何给出
- (C++)我的函数不返回数组
- 如何让我的重载<<运算符打印出我的函数?
- 我的函数调用 (C++) 中的未声明标识符
- 钩/绕道 d3d9 (现在/结束场景) - 似乎调用我的函数然后崩溃
- 成员函数的"this"参数具有"const"类型,但我的函数实际上不是"const"
- 我应该声明我的函数模板专业化还是定义它们就足够了
- 为什么我的函数名称中会随机出现一个额外的字母
- 为什么我的函数无法使用指针打印多维数组的元素?
- 当我从头文件和实现文件调用我的函数到我的主文件时,我没有得到任何输出
- 钩子SSDT.其他驱动程序使用我的函数
- 如何修复我的函数 popFront 为我的列表?