【问题标题】:Passing a 2D array through a function and I get an error通过函数传递二维数组,我得到一个错误
【发布时间】:2013-12-06 04:17:22
【问题描述】:

我正在制作一个递归算法,作为班级额外学分项目的一部分,以通过 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> 从整数类型转换为指针类型需要 reinterpret_cast、C 样式转换或函数样式转换 1>c:\用户\贾斯汀\文档\视觉工作室 2012\projects\project5\project5\source.cpp(114):错误 C2664: 'pathfinder' : 无法将参数 1 从 'int' 转换为 'int [][20]' 1> 从整数类型转换为指针类型需要 reinterpret_cast、C 样式转换或函数样式转换 ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========

【问题讨论】:

    标签: c++ arrays casting


    【解决方案1】:

    这些都不正确:

    #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 上完成大部分工作,从而使参数无用。

    【讨论】:

    • 感谢您的回答 :-) 嗯,我不太了解您的第二部分,但我的编译问题并没有消失......也许是视觉工作室?
    • 您确定上面问题中的代码是您要编译的代码吗?当我对上面推荐的 SIZEX/SIZEY 进行修复时,它修复了编译器错误。第二部分:我是说当你递归时,你应该将a 传递给pathfinder 内部的递归调用,而不是arr。想一想如果你有两个不同的数组arr1arr2 会发生什么,并且你希望main 调用pathfinder 来解决这两个问题。
    • 我 100% 确定上面的代码是我要编译的,当你运行它时,它是否返回一个 2D int 数组?它有效吗?哦,我明白你的意思了,关于 a
    • 是的,它编译并运行到完成,打印出一个 20 x 5 的数字表。您是否尝试过#define 方法、const int 方法或两者兼而有之?修复#define后,我都尝试了,并且两者都可以工作。
    猜你喜欢
    • 2021-03-24
    • 2012-08-05
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 2012-08-14
    相关资源
    最近更新 更多