3

我正在尝试解决未解决的外部问题(link2019 错误)。StackOverflow 上有很多关于这个问题的帖子,但要么我不理解错误,要么我对此视而不见。

该错误是由我的 generate_maze 函数引起的(特别是由 rand_neighbor() 调用,对吗?),但我的理解是这些都已“解决”。

我稍微截断了代码,因为它非常冗长。我希望这是适当的。

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 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
4

2 回答 2

4

在这里使用不错的 web c++ demangler ,您可以看到您的未定义引用?rand_neighbor@@YA?AV?$Vector@H@@V1@AAV?$Grid@_N@@@Z实际上意味着class Vector __cdecl rand_neighbor(class Vector,class Grid &). 您的错误消息中缺少参数。

现在,您看到函数的声明和定义之间的区别了吗?

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

让我把它们标准化一下:

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

您忘记了&函数原型中的引用 ()!因此,您的定义是不同的功能。

于 2013-03-14T23:10:33.367 回答
3

正如链接器告诉你的那样,问题出在rand_neighbor()函数上。你为它提供了一个声明(如果你不提供,你会得到一个编译器错误而不是一个链接器错误),但是你没有提供一个定义

于 2013-03-14T22:47:39.377 回答