相关疑难解决方法(0)

使用模板时,为什么会出现"未解析的外部符号"错误?

当我使用模板为类编写C++代码并在源(CPP)文件和标题(H)文件之间拆分代码时,在链接最终可执行文件时会出现大量"未解析的外部符号"错误,尽管目标文件正确构建并包含在链接中.这里发生了什么,我该如何解决?

c++ linker templates

92
推荐指数
2
解决办法
6万
查看次数

未定义的模板成员引用

我是C++的新手,在Ubuntu 10.04上使用NetBeans IDE准备作业.我使用g ++作为C++编译器.

错误消息:

build/Debug/GNU-Linux-x86/Maze.o: In function `Maze':
Maze/Maze.cpp:14: undefined reference to `Stack<Coordinate>::Stack()'
Maze/Maze.cpp:14: undefined reference to `Stack<Coordinate>::Stack()'
Maze/Maze.cpp:69: undefined reference to `Stack<Coordinate>::push(Coordinate)'
Maze/Maze.cpp:79: undefined reference to `Stack<Coordinate>::isEmpty()'
Maze/Maze.cpp:87: undefined reference to `Stack<Coordinate>::destroy()'
Run Code Online (Sandbox Code Playgroud)

我的相关代码:

Maze.h

#include "Coordinate.h"
#include "Stack.h"
....
....
/**
 * Contains the stack object
 *
 * @var  Stack stack
 * @access private
 */
Stack<Coordinate> *stack;
...
...
Run Code Online (Sandbox Code Playgroud)

Maze.cpp

#include "Maze.h"
...
...
Maze::Maze()
{
    // IT SHOWS THAT THE FOLLOWING LINE HAS AN ERROR///
    stack = …
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates undefined-reference

3
推荐指数
1
解决办法
3602
查看次数

标签 统计

c++ ×2

templates ×2

gcc ×1

linker ×1

undefined-reference ×1