我正在尝试解决未解决的外部问题(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
答案 0 :(得分:4)
使用漂亮的网络c ++ demangler here,您可以看到未定义的引用?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> &) { /* ... */}
你忘记了函数原型中的引用(&
)!因此,您的定义具有不同的功能。
答案 1 :(得分:3)
正如链接器告诉你的那样,问题出在rand_neighbor()
函数上。你为它提供了声明(如果你没有,你会得到编译器错误而不是链接器错误),但你还没有提供定义。