代码块在程序中显示错误,但我找不到它
codeblocks shows error in a program but i can't find it
void PRINT_LCS(int b[][], string x, int i, int j){
if(i==0 || j==0)
cout<<x[0];
if(b[i][j] == 1){
PRINT_LCS(b, x, i-1, j-1);
cout<<x[i];
}
else if(b[i][j] == 2)
PRINT_LCS(b, x, i-1, j-1);
else
PRINT_LCS(b, x, i, j-1);
}
这是我的程序,但我不知道为什么第一行有错误。错误消息如下所示:
error: declaration of 'b' as multidimensional array must have bounds for all dimensions except the first|
error: expected ')' before ',' token|
error: expected initializer before 'x'|
在函数参数中声明数组时,必须传递 2D 数组的第二个(列)维度。在您的情况下:
void PRINT_LCS(int b[][COLUMN], string x, int i, int j) //replace column with the no of cols in your 2D array
当你将一个一维数组声明为 int b[]
时,编译器不知道该数组的大小,但它使用 b 作为 int 的指针并能够访问其组件,程序员有责任确保访问在数组的边界内。所以,这段代码:
void f(int b[]) {
b[5] = 15; // access to 5th element and set value 15
}
相当于:
void f(int *b) {
*(b + 5) = 15;
}
如果使用二维数组,则数组的行将连续存储在内存中,因此编译器需要知道列大小才能访问数组中的任意元素。(程序员仍然有责任确保访问没有越界)。现在,这段代码:
void f(int b[][COLUMN_SIZE]) {
b[5][3] = 15; // access to 5th row, 3rd column and set value 15
}
相当于:
void f(int *b) {
*(b + 5 * COLUMN_SIZE + 3) = 15;
}
但是,如果不指定列大小,编译器如何知道其大小?这对于多维数组是可推广的。
相关文章:
- 我在c ++矩阵计算器程序中找不到错误
- 我在代码中找不到错误以找到最大的素因数?
- 在我的应用逻辑中找不到错误
- 在模板类中找不到错误
- 双重释放或损坏错误(找不到错误?
- 在我的 cpp 或头文件中找不到错误,类构建失败
- 在哈希表复制构造函数中找不到错误
- 找不到错误。C++,字符串
- 8谜题代码.我的队列没有填充,我找不到错误的位置
- C++中的预期表达式,但找不到错误
- C++ 将布尔隐式转换为 int 和算术,删除分支,找不到错误
- 找不到错误"invalid conversion from char to const char*"的解决方案
- 在以下代码中找不到错误:- 在 try() 之前预期未限定 id
- 在重载运算符中找不到错误<
- C++ 中的分段错误(核心转储) 找不到错误
- 在使用set_difference算法时找不到错误
- 在 while 循环中找不到错误?
- C++错误LNK2001,找不到错误
- 在我的代码中找不到错误....分段错误
- LNK2005(已定义)找不到错误