尝试通过引用传递的错误预期为“)" [英] Error expected ')' trying to pass by reference
问题描述
我正在尝试使用递归函数计算矩阵中连接的1的数量.递归地通过大小(max_sz)传递给我一个问题,但是我不明白为什么它说我缺少括号.
I'm trying to count the number of connected 1's in a matrix using a recursive function. Passing the size (max_sz) by reference recursively is giving me problems, but I don't understand why it's saying I'm missing a parenthesis.
我已经看了30分钟了,不知道为什么我得到这个错误.语法对我来说似乎不错,但是我是C入门者,所以我想我遇到了以前从未见过的事情(或者我只是盲目).
I've been looking at this for 30 minutes and have no idea why I'm getting this error. The syntax looks right to me, but I'm a C-beginner so I'm guessing I ran into something I haven't seen before (or I'm just blind).
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int getval(int (*A)[5], int i, int j, int L, int H) {
if(i<0 || i>=L || j<0 || j>=H)
return 0;
else {
return A[i][j];
}
}
void findMaxBlock(int (*A)[5], int r, int c, int L, int H, int size, int **cntarr, int &max_sz){
if(r>=L || c >=H)
return;
cntarr[r][c]=1;
size++;
if(size>max_sz)
max_sz = size;
//search in eight directions
int direction[][2]={{-1,0},{-1,-1},{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1}};
for(int i=0; i<8; i++) {
int newi = r+direction[i][0];
int newj = c+direction[i][1];
int val = getval(A, newi, newj, L, H);
if(val>0 && (cntarr[newi][newj]==0)){
findMaxBlock(A, newi, newj, L, H, size, cntarr, max_sz);
}
}
cntarr[r][c]=0;
}
int** create2darr(int rmax, int colmax) { // In C99, you can use variable-length arrays:
int **mat = (int**)malloc(rmax*sizeof(int*));
for(int i = 0; i < rmax; i++) mat[i] = (int*)malloc(colmax * sizeof(int));
return mat;
}
int getMaxOnes(int (*A)[5], int rmax, int colmax) {
int max_sz = 0;
int size = 0;
int **cntarr = create2darr(rmax, colmax);
for(int i=0; i<rmax; i++){
for(int j=0; j<colmax; j++){
if(A[i][j] == 1){
findMaxBlock(A, i, j, rmax, colmax, 0, cntarr, max_sz);
}
}
}
return max_sz;
}
int main(int argc, char *argv[]) {
int zarr[][5] = {{1,1,0,0,0},{0,1,1,0,0},{0,0,1,0,1},{1,0,0,0,1},{0,1,1,1,1}};
printf("Number of maximum 1s are %d\n", getMaxOnes(zarr, 5, 5));
}
/*
11000
01100
00101
10001
01111
ans: 7 (diagonals count too)
*/
以下是生成的错误:
connected1sInMatrix.c:14:88: error: expected ')'
void findMaxBlock(int (*A)[5], int r, int c, int L, int H, int size, int **cntarr, int &max_sz){
^
connected1sInMatrix.c:14:18: note: to match this '('
void findMaxBlock(int (*A)[5], int r, int c, int L, int H, int size, int **cntarr, int &max_sz){
^
connected1sInMatrix.c:14:88: error: parameter name omitted
void findMaxBlock(int (*A)[5], int r, int c, int L, int H, int size, int **cntarr, int &max_sz){
^
connected1sInMatrix.c:19:10: error: use of undeclared identifier 'max_sz'
if(size>max_sz)
^
connected1sInMatrix.c:20:3: error: use of undeclared identifier 'max_sz'
max_sz = size;
^
connected1sInMatrix.c:28:52: error: use of undeclared identifier 'max_sz'
findMaxBlock(A, newi, newj, L, H, size, cntarr, max_sz);
^
5 errors generated.
我知道stackoverflow不是调试器,但是我完全不知道这里发生了什么.任何意见或建议,不胜感激,谢谢.
I know stackoverflow is not a debugger, but I'm completely lost as to what's going on here. Any advice or suggestions is greatly appreciated, thanks.
推荐答案
问题在这里:
void findMaxBlock(int (*A)[5], int r, int c, int L,
int H, int size, int **cntarr, int &max_sz){
// ^----- here
您正试图将参数声明为引用,这是仅C ++的功能.
You're trying to declare a parameter as a reference, which is a C++ only feature.
将其更改为 * max_sz
,并相应地取消引用.
Change it to *max_sz
, and dereference it accordingly.
在 findMaxBlock
中:
...
if(size>*max_sz)
*max_sz = size;
...
findMaxBlock(A, newi, newj, L, H, size, cntarr, max_sz);
在 main
中:
findMaxBlock(A, i, j, rmax, colmax, 0, cntarr, &max_sz);
这篇关于尝试通过引用传递的错误预期为“)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!