对模板成员的未定义引用

发布于 2024-10-01 04:41:44 字数 2073 浏览 1 评论 0原文

我是 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()'

我的相关代码:

Maze.h

#include "Coordinate.h"
#include "Stack.h"
....
....
/**
 * Contains the stack object
 *
 * @var  Stack stack
 * @access private
 */
Stack<Coordinate> *stack;
...
...

Maze.cpp

#include "Maze.h"
...
...
Maze::Maze()
{
    // IT SHOWS THAT THE FOLLOWING LINE HAS AN ERROR///
    stack = new Stack<Coordinate>;
    ///////////////////////////////////////////////////

    for( int y=0; y<8; y++ )
    {
        for( int x=0; x<8; x++ )
        {
            maze[y][x] = '0';
        }
    }
}
...
...

并且根据错误输出,我使用 stack 变量的每一行都有一个错误:未定义的引用。

Stack.cpp

#include "Stack.h"
...
...
template <class T> Stack<T>::Stack()
{
    // Create the stac!
    create();
}
...

我用谷歌搜索过,但无法解决问题。我认为我的包含顺序有问题,或者我可能以错误的方式使用了指针。

我也尝试自己创建makefile,但结果并没有改变。我根据以下链接准备了这个 makefile: http://www. cs.umd.edu/class/spring2002/cmsc214/Tutorial/makefile.html

这是我的 makefile:

maze: Maze.o Stack.o Coordinate.o
 g++ -Wall Maze.o Stack.o Coordinate.o -o maze

Maze.o: Maze.cpp Maze.h Stack.h Coordinate.h
 g++ -Wall -c Maze.cpp

Stack.o: Stack.cpp Stack.h
 g++ -Wall -c Stack.cpp

Coordinate.o: Coordinate.cpp Coordinate.h
 g++ -Wall -c Coordinate.cpp

Maze.h: Stack.h Coordinate.h

我怎样才能克服这个错误?有什么想法吗?

I'm new to C++, and preparing a homework by using NetBeans IDE on Ubuntu 10.04. I use g++ as a C++ compiler.

The error message:

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()'

And my related code:

Maze.h

#include "Coordinate.h"
#include "Stack.h"
....
....
/**
 * Contains the stack object
 *
 * @var  Stack stack
 * @access private
 */
Stack<Coordinate> *stack;
...
...

Maze.cpp

#include "Maze.h"
...
...
Maze::Maze()
{
    // IT SHOWS THAT THE FOLLOWING LINE HAS AN ERROR///
    stack = new Stack<Coordinate>;
    ///////////////////////////////////////////////////

    for( int y=0; y<8; y++ )
    {
        for( int x=0; x<8; x++ )
        {
            maze[y][x] = '0';
        }
    }
}
...
...

And according to the error output, Each line that I used stack variable has an error: Undefined reference.

Stack.cpp

#include "Stack.h"
...
...
template <class T> Stack<T>::Stack()
{
    // Create the stac!
    create();
}
...

I have googled it, but could not solve the problem. I think there is something wrong with my includes order, or maybe I used the pointers in a wrong way.

I also tried to create a makefile by myself, but the result did not change. I prepared this makefile according to this link: http://www.cs.umd.edu/class/spring2002/cmsc214/Tutorial/makefile.html

Here is my makefile:

maze: Maze.o Stack.o Coordinate.o
 g++ -Wall Maze.o Stack.o Coordinate.o -o maze

Maze.o: Maze.cpp Maze.h Stack.h Coordinate.h
 g++ -Wall -c Maze.cpp

Stack.o: Stack.cpp Stack.h
 g++ -Wall -c Stack.cpp

Coordinate.o: Coordinate.cpp Coordinate.h
 g++ -Wall -c Coordinate.cpp

Maze.h: Stack.h Coordinate.h

How could I overcome this error? Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糯酥胸 2024-10-08 04:41:44

堆栈是一个模板。完整的定义必须放在其头文件中。也就是说,不要将其分成 .h 和 .cpp 文件。

Stack is a template. The complete definition has to go in its header file. That is, do not separate it into a .h and a .cpp file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文