输出 [left]=input[i] 行中的 c++ 代码中存在无效的类型错误
Invalid type error in c++ code in output[left]=input[i] line
我得到了这个
'invalid types int*[std::ios_based&(std::ios_base&)]' for array subscript in line
output[left]=input[i];
无法找出解决方案。 这里的输出是我想要答案的数组
#include <bits/stdc++.h>
using namespace std;
void recover(int input[], int output[], int n)
{
int i = 0;
int mid = n / 2;
if (n % 2 != 0)
{
output[mid] = input[i];
int left = mid - 1;
int right = mid + 1;
i++;
}
else
{
int left = mid - 1;
int right = mid;
}
while (i < n)
{
output[left] = input[i];
left--;
i++;
output[right] = input[i];
right++;
i++;
}
}
int main()
{
int input[100], output[100];
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> input[i];
}
recover(input, output, n) for (int i = 0; i < n; i++)
{
cout << output[i] << " ";
}
}
首先,正如Richard Critten在评论中指出的那样,您有一个范围问题。right
和left
在while
循环中没有您在if
和else
语句中给出的定义。
雪上加霜的是,您使用#include <bits/stdc++.h>
和using namespace std;
.这些不会导致您的问题,但它们使问题更难理解。
使用#include <bits/stdc++.h>
是一个坏主意。它包括整个C++标准库,这是懒惰的,不必要的,并且增加了编译时间。它也不是便携式的。
使用using namespace std;
是一个坏主意。它使命名空间中的所有内容std
无需限定即可使用。这意味着任何常用名称(如left
和right
(都可能与您不想要的内容匹配。这可能会导致非常难以找到和修复的错误。
同时使用两者真的很糟糕,因为现在你拥有了整个std
命名空间,有很多常用名称,与你想要在代码中使用的名称发生冲突。
相关文章:
- 为什么此代码存在运行时错误?
- 为什么以下C++代码中存在内存泄漏?
- 如何优化代码以返回最接近给定整数的数字,但给定列表中不存在?
- 我遇到了这个代码片段,不明白. 它递归检查 C++ 字符串中是否存在大写字符
- 以下代码中存在哪些问题?
- 为什么此代码中存在分段错误?
- 无法在这个基本的Qt代码片段中找到错误,但我被告知它肯定存在?
- 当存在覆盖歧义函数时,代码如何运行?
- 基于构造函数的存在禁用代码
- 输出 [left]=input[i] 行中的 c++ 代码中存在无效的类型错误
- 以下代码是否存在内存泄漏?
- 我的代码似乎在查找最大数字时存在语法错误
- 如何通过用C++编写的代码将位图图像保存在Android设备中
- 以下代码存在哪些问题?
- 我的选择排序代码是否存在导致它跳过数组中的元素的问题?
- 在存在错误代码的情况下输出参数与 NRVO
- 代码中存在多个错误
- 存在内存访问异常,但我不确定我的代码中出了什么问题
- 一些人发现此代码存在溢出问题.小的数字是非常负的
- 如何让代码存在于两个或多个非嵌套命名空间的范围内