Цветан Коев
Цветан Коев

Reputation: 13

error C2664: 'way' : cannot convert parameter 4 from 'int' to 'int &'

I'm having problem with a code.

This is my main.

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;
}

This is the function.

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;
}

The code can't compile. It says

1>c:\users\admin\documents\zad51.cpp(102): error C2664: 'way' : cannot convert parameter 4 from 'int' to 'int &'

Upvotes: 1

Views: 2949

Answers (3)

David G
David G

Reputation: 96865

The way function takes a reference to an integer as its fourth argument. You can't pass a literal (aka rvalue). A reference to an object is always an lvalue or a variable. What you should do is make a variable and pass that to the function call instead.

Upvotes: 1

Michał Szczygieł
Michał Szczygieł

Reputation: 399

Because you try to pass constant literal as fourth argument. Assign it to variable i.e.:

int variable = 0;
way(x, y, crrWay, variable, 0, minWay, minWayN, 0);

Upvotes: 1

juanchopanza
juanchopanza

Reputation: 227608

Your way function takes non-const references int&, and you are passing temporaries such as 0. Non-const references cannot bind to temporaries. You need to change the signature to take const references, or, if the function actually mutates the ints refered to, do not pass temporaries.

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

or

int a = 0;
int b = 0;
int c = 0;
way(x, y, crrWay, a, b, minWay, minWayN, c);

Upvotes: 5

Related Questions