当我在函数参数中使用指针时,我遇到了错误
when i was using pointers in function arguments i faced with an error
错误是这样:无法转换int*' to
INT*',用于参数1' to
BOOL Permition(INT *,INT,INT)'在代码中,我有一个int板[n],用户给了'n'...我想在此数组中赋予我的验证功能,所以我必须用指针给它,因为未指定它的长度...所以我该如何解决此问题这是我的代码:
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
bool permition(int* board[],int place,int n_){
int m=place;
while(m!=0){
m--;
if(abs(abs(board[m]-board[place])-abs(m-place))==1
&& abs(m-place)<3 && abs(board[m]-board[place]))
return false;
}
return true;
}
void printBoard(int* board[],int n){
for(int i=0;i<n;i++)
cout << board[i]<< " ";
cout << endl;
}
int main()
{
int p=0;
int n;
cout << "plz: ";
cin >> n;
int board[n];
for(int i=0;i<n;i++)
board[i]=0;
while(p<n){
while((board[p]<n) && permition(board,p,n)==false)
board[p]+=1;
if(board[p]<n)
p++;
else{
p--;
board[p]+=1;
}
if(p==n && board[0]<n-1)
//it means the first number is not max so we should
//print and continue from fist again
{
printBoard(board,n);
p=0;
board[0]+=1;
}
}
system("PAUSE");
return 0;
}
在验证的功能定义中摆脱[]:
bool permition(int* board,int place,int n_)
看看是否有帮助!
更改
bool permition(int* board[],int place,int n_)
to
bool permition(int const board[],int place,int n_)
当前的参数声明(上面首先)说 board
是 int
的指针数组,不是。
[]
最终是指针,因此您可以写
bool permition(int const* board,int place,int n_)
此表格的优势是您也可以在指针上使用const
,而使用[]
,您有一个可以更改的指针,但看起来像一个数组。
缺点是声明不再向读者传达"数组"。
正如其他人所指出的,原始代码将int
的数组(被视为int *
)传递到被声明将A Pointer 的函数传递给一个函数int
的数组。这是一个更正的版本,我希望有一些格式化更改。
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
// Removed unused parameter n_.
bool permission(int board[], int place) {
int m = place;
// The original code could return false only if (place - m) < 3,
// so no need to test when (place - m) >= 3.
while (m-- > max(0, place - 3) {
int board_diff = abs(board[m] - board[place];
int index_diff = place - m; // Always >= 0
if (abs(board_diff - index_diff) == 1 && board_diff != 0) {
return false;
}
}
return true;
}
void printBoard(int board[], int n) {
for (int i = 0; i < n; i++) {
cout << board[i] << " ";
}
cout << endl;
}
int main() {
int p = 0;
int n;
cout << "plz: ";
cin >> n;
int board[n] = { 0 }; // Zeroes entire array.
while (p < n) {
// Never compare bool to true or false; just use !bool_var.
while ((board[p] < n) && !permission(board, p)) {
board[p]++;
}
if (board[p] < n) {
p++;
} else {
board[--p]++;
}
if (p == n && board[0] < (n - 1)) {
// The first number is not max so we should
// print and continue from first again
printBoard(board, n);
board[p = 0]++; // Assign and increment.
}
}
system("PAUSE");
return 0;
}
int board[];
与
相同 int *board;
所以我猜测的解决方案是写
bool permition(int board[],int place,int n_)
void printBoard(int board[],int n)
您需要了解并在指针上进行更多工作!
相关文章:
- 我在 C++ 代码中遇到错误警告:控制到达非空函数 [-Wreturn 类型] 的末尾
- 我在 ndk 中构建文件时遇到错误 android.mk
- 我们在批处理模式下使用 G++ 时遇到错误
- 在visual Studio中构建代码时,我遇到错误,.h文件丢失
- 学习C++并在早期示例中遇到错误(在非静态数据成员之前需要构造函数)
- 可中断线程类 C++11 - 遇到错误?
- 在尝试在同一类内部进行课程矢量时,我会遇到错误
- 使用向量和结构在C 中遇到错误
- 我正在尝试安装游戏引擎,但遇到错误
- 使用 bazel 从源代码构建张量流服务遇到错误:C++规则'@org_tensorflow//…'编译失败(出口 4)
- 为什么尝试这样的类代码时会遇到错误?我初学者对不起
- 当 Mingw 为 C++ 编译 OpenCV 时,我遇到错误?
- 遇到错误试图在功能型参数中使用void函数的错误
- 尝试使用Boost和Ncurses库进行编译程序时会遇到错误
- 我在使用C 的Hangman游戏中遇到错误
- 为什么我在加载共享库时遇到错误
- 我在 c++ 中遇到错误'PTHREAD_START_ROUTINE'未在此范围内声明
- 学习如何在遇到错误'variable not declared in scope'的 c++ 中使用指针
- 如果遇到错误,如何重复代码
- 当我尝试将UDP套接字绑定到带有SFML的端口时,为什么会遇到错误