我是一名新手C++程序员,正在使用Ubuntu 10.04上的NetBeans IDE准备一份作业。我使用g++作为C++编译器。
错误信息:
根据错误输出,我使用的每一行stack变量都有错误:未定义的引用。
我已经谷歌搜索过了,但无法解决问题。我认为我的包含顺序有问题,或者可能我使用指针的方法不正确。
我还尝试自己创建一个makefile,但结果没有改变。我根据此链接准备了这个makefile:http://www.cs.umd.edu/class/spring2002/cmsc214/Tutorial/makefile.html 以下是我的makefile:
我该如何克服这个错误?有什么想法吗?
错误信息:
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';
}
}
}
...
...
根据错误输出,我使用的每一行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
我该如何克服这个错误?有什么想法吗?