1

我的代码有问题。

这是我的主要内容。

int main()
{
    cin>>xG>>yG;
    int x,y;
    cin>>x>>y;
    int crrWay[200] = {0},
        minWay[200] = {0},
        minWayN = -1;
    way(x, y, crrWay, 0, 0, minWay, minWayN, 0);
    printWay(minWay, minWayN);
    return 0;
}

这就是功能。

void way(int x, int y, int *crrWay, int& crrWayWeight, int l, int* minWay, int& minWayN, int& minWayWeight)
{
    crrWay[2*l] = x;
    crrWay[2*l+1] = y;

    if( x < 0 || y < 0 || x > 10 || y > 10 )
        return;

    // Сравнява намерения път с минималния
    if(x == xG && y == xG)
    {
        registerWay(crrWay, l+1, minWay, minWayN, crrWayWeight, minWayWeight);
        return;
    }

    // Клетката е непроходима.
    if(tempMaze[x][y]==0)
        return;
    tempMaze[x][y] = 0;
    crrWayWeight+=maze[x][y];

    // Рекурсивни обръщения към всеки от четирите съседа
    way(x+1, y, crrWay, crrWayWeight, l+1, minWay, minWayN, minWayWeight);
    way(x, y+1, crrWay, crrWayWeight, l+1, minWay, minWayN, minWayWeight);
    way(x-1, y, crrWay, crrWayWeight, l+1, minWay, minWayN, minWayWeight);
    way(x, y-1, crrWay, crrWayWeight, l+1, minWay, minWayN, minWayWeight);

    // връщане назад
    tempMaze[x][y] = 1;
}

代码无法编译。它说

1>c:\users\admin\documents\zad51.cpp(102): 错误 C2664: 'way' : 无法将参数 4 从 'int' 转换为 'int &'

4

3 回答 3

5

您的way函数采用非常量引用int&,并且您正在传递诸如0. 非常量引用不能绑定到临时对象。您需要更改签名以获取 const 引用,或者,如果函数实际上改变了所引用的整数,则不要传递临时变量。

void way(int x, int y, int *crrWay, const int& crrWayWeight, int l, int* minWay, const int& minWayN, const int& minWayWeight);

或者

int a = 0;
int b = 0;
int c = 0;
way(x, y, crrWay, a, b, minWay, minWayN, c);
于 2013-01-15T22:32:28.580 回答
1

way函数将整数的引用作为其第四个参数。您不能传递文字(又名右值)。对对象的引用始终是左值或变量。您应该做的是创建一个变量并将其传递给函数调用。

于 2013-01-15T22:32:59.653 回答
1

因为您尝试将常量文字作为第四个参数传递。将其分配给变量,即:

int variable = 0;
way(x, y, crrWay, variable, 0, minWay, minWayN, 0);
于 2013-01-15T22:33:46.670 回答