通过一个函数传递一个2D数组,我得到一个错误

Passing a 2D array through a function and I get an error

本文关键字:一个 数组 错误 函数 2D      更新时间:2023-10-16

我正在制作一个递归算法,作为班级额外学分项目的一部分,以找到通过0和1的2D int数组迷宫的最短路径。0表示墙,1表示可以穿过的走廊。我认为我拥有所有完美的东西,但它就是无法编译。它说我正在尝试将一些东西从int转换为array或其他什么。这是我的代码,请帮忙。

#include <iostream> 
using namespace std;    
#define 20 SIZEX;   
#define 5 SIZEY;
int value; //to compare paths to take
int starti = 1;
int startj = 0;
int newi;
int newj;
int counter = 0; //keeps track of how many steps taken
void pathfinder(int a[][SIZEX], int currenti, int currentj);
    int arr[SIZEY][SIZEX] =         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,       
                                     1,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,   
                                     0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,   
                                     0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
                                     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};  
int main()
{    
    pathfinder(arr, starti, startj);
    system("PAUSE");
    return 0;
}
void pathfinder(int a[][SIZEX], int currenti, int currentj)
{
    //as soon as it walks somewhere, the value of that spot increments
    int temp;
    temp = a[currenti][currentj];
    temp++;
    a[currenti][currentj] = temp;
    if (counter == 0) //keeps track of starting point
    {
            starti = currenti;
        startj = currentj;
    }
    if (currenti-1 >= 0 && a[currenti-1][currentj] != 0) //checks up
     {
        value = a[currenti-1][currentj];
     }
    else if (currenti+1 < 5 && a[currenti+1][currentj] != 0) //checks down
    {
        value = a[currenti+1][currentj];
    }
    else if (currentj-1 >= 0 && a[currenti][currentj-1] != 0) //checks left
    {
        value = a[currenti][currentj-1];
    }
    else if (currentj+1 < 20 && a[currenti][currentj+1] != 0) //checks right
    {
        value = a[currenti][currentj+1];
    }
    //value has a value, now check it against all possible values for the least travelled path
    if ((currenti-1 >= 0 && a[currenti-1][currentj] !=0) && value > a[currenti-1][currentj])
    {
        value = a[currenti-1][currentj];
    }
     if ((currenti+1 < 5 && a[currenti+1][currentj] !=0) && value > a[currenti+1][currentj])
    {
        value = a[currenti+1][currentj];
    }
    if ((currentj-1 >= 0 && a[currenti][currentj-1] != 0) && value > a[currenti][currentj-1]) 
     {
         value = a[currenti][currentj-1];
     }
    if ((currentj+1 < 20 && a[currenti][currentj+1] != 0) && value > a[currenti][currentj+1])
    {
        value = a[currenti][currentj+1];
    }
//value now holds the smallest possible value among the four possible paths
if ((currenti-1 >= 0 && a[currenti-1][currentj] !=0) && value == a[currenti-1][currentj]) //move up
{
    newi = currenti-1;
    newj = currentj;
    counter++;
}
else if ((currenti+1 < 5 && a[currenti+1][currentj] !=0) && value == a[currenti+1][currentj]) //move down
{
    newi = currenti+1;
    newj = currentj;
    counter++;
}
else if ((currentj-1 >= 0 && a[currenti][currentj-1] != 0) && value == a[currenti][currentj-1]) //move left
{
    newi = currenti;
    newj = currentj-1;
    counter++;
}
else if ((currentj+1 < 20 && a[currenti][currentj+1] != 0) && value == a[currenti][currentj+1]) //move right
{
    newi = currenti;
    newj = currentj+1;
    counter++;
}
//upon reaching the exit, it will print out a new 2d maze, and the path with the smallest value of non-zero integers is the shortest path
if ((currenti == 0 || currentj == 0 || currenti == 4 || currentj == 19) && (currenti != starti || currentj !=startj))
{
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0;j < 20;j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    return;
}
pathfinder(arr, newi, newj);
}

1> ------生成已启动:项目:Project5,配置:调试Win32------1>Source.cpp 1>c:\users\justin\documents\visual studio 2012\projects\project5\project5\Source.cpp(22):错误C2664:"pathfinder":无法将参数1从"int"转换为"int[][20]"1> 从积分类型转换为指针类型需要interpret_cast、C样式强制转换或函数样式强制转换1> c:\users\justin\documents\visualstudio2012\projects\project5\project5\source.cpp(114):错误C2664:"pathfinder":无法将参数1从"int"转换为"int[][20]"1> 从积分类型转换为指针类型需要interpret_cast、C样式强制转换或函数样式强制转换==========生成:0成功,1失败,0最新,0跳过==========

这些是不正确的:

#define 20 SIZEX;   
#define 5 SIZEY;

如果你真的想要宏,定义它们的正确方法是:

#define SIZEX 20
#define SIZEY 5

但是,由于这是C++,您应该在这里使用const int

const int SIZEX = 20;
const int SIZEY = 5;

这应该可以解决编译问题。

另请注意:如果要将arr作为初始参数传递给pathfinder()以在main中开始递归,则可以更改递归调用以传递传递pathfinder()的参数。即:

pathfinder(a, newi, newj);

否则,即使main将不同的数组传递给pathfinder,它仍然会在arr上完成大部分工作,从而使参数变得无用。

相关文章: