我遇到代码问题。
这是我的主要内容。
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&gt; c:\ users \ admin \ documents \ zad51.cpp(102):错误C2664:'way':不能 将参数4从'int'转换为'int&amp;'
答案 0 :(得分:5)
您的way
函数采用非常量引用int&
,并且您正在传递0
等临时值。非const引用不能绑定到临时对象。你需要更改签名以获取const引用,或者,如果函数实际上改变了所引用的int,则不要传递临时值。
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);
答案 1 :(得分:1)
way
函数将整数引用作为其第四个参数。你不能传递文字(也就是右边)。对对象的引用始终是左值或变量。你应该做的是创建一个变量并将其传递给函数调用。
答案 2 :(得分:1)
因为您尝试将常量文字作为第四个参数传递。将其分配给变量即:
int variable = 0;
way(x, y, crrWay, variable, 0, minWay, minWayN, 0);