unresolved external link2019 confusion

Tyler Seymour picture Tyler Seymour · Mar 14, 2013 · Viewed 11.2k times · Source

I'm trying to resolve an unresolved external (link2019 error). There are many posts on StackOverflow about this issue, but either I am not understanding the error or I am blind to it.

The error is caused by my generate_maze function (specifically by the rand_neighbor() call, right?) but my understanding is that these are all "resolved".

I truncated the code a little bit because it is quite verbose. I hope this was appropriate.

void generate_maze (Vector<int> &coords, Grid<bool> &included, Maze &m);

int main() {

    Grid<bool> included = initialize_grid();
    Vector <int> coords = rand_coords();
    Vector <int> current_point = coords;

    generate_maze(coords, included, m);
    return 0;
}

void generate_maze (Vector<int> &coords, Grid<bool> &included,  Maze &m) {
    while (gridIsTrue == false) {
    Vector<int> neighbor = rand_neighbor(coords, included);
    pointT neighborpoint = {neighbor[0], neighbor[1]};
    pointT current_point = {coords[0], coords[1]};
    if (included.get(neighbor[0], neighbor[1]) == false) {m.setWall(current_point, neighborpoint, false); included.set(neighbor[0], neighbor[1], true); current_point = neighborpoint;}
    }
}

Vector<int> rand_neighbor(Vector<int> &coords, Grid<bool> &included) {
    while (1) {
        int randomint;
        randomint = randomInteger(1,4);
        if (randomint == 1) {if (included.inBounds(coords[0], coords[1]+1)) {coords[1] = coords[1]+1; break;}}
        if (randomint == 2) {if (included.inBounds(coords[0], coords[1]-1)){coords[1] = coords[1] -1; break;}}
        if (randomint == 3) {if (included.inBounds(coords[0] -1, coords[1])){coords[0] = coords[0] -1; break;}}
        if (randomint == 4) {if (included.inBounds(coords[0] +1, coords[1])){coords[0] = coords[0] + 1; break;}}
                }
        return coords;

Error:

error LNK2019: unresolved external symbol "class Vector<int> __cdecl rand_neighbor(class Vector<int>,class Grid<bool> &)" (?rand_neighbor@@YA?AV?$Vector@H@@V1@AAV?$Grid@_N@@@Z) referenced in function "void __cdecl generate_maze(class Vector<int> &,class Grid<bool> &,class Maze &)" (?generate_maze@@YAXAAV?$Vector@H@@AAV?$Grid@_N@@AAVMaze@@@Z)
1>C:\Users\com-user\Desktop\New folder\maze\assign3-maze-PC\Maze\Debug\Maze.exe : fatal error LNK1120: 1 unresolved externals

Answer

rodrigo picture rodrigo · Mar 15, 2013

Using the nice web c++ demangler here you can see that your undefined reference ?rand_neighbor@@YA?AV?$Vector@H@@V1@AAV?$Grid@_N@@@Z actually means class Vector __cdecl rand_neighbor(class Vector,class Grid &). The parameters are missing from your error message.

Now, do you see the difference between the declaration and the definition of your function?

class Vector __cdelc rand_neighbor(class Vector,class Grid &);
Vector<int> rand_neighbor(Vector<int> &coords, Grid<bool> &included) { /* ... */}

Let me normalize them a bit:

Vector<int> rand_neighbor(Vector<int>, Grid<bool> &);
Vector<int> rand_neighbor(Vector<int> &, Grid<bool> &) { /* ... */}

You forgot a reference (&) in the prototype of the function! Thus, your definition is of a different function.