Undefined template member reference

I am new to C ++ and preparing my homework using the NetBeans IDE on Ubuntu 10.04. I use g ++ as a C ++ compiler.

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 in accordance with the result of the error, each line in which I used the variable stack has an error: Undefined reference.

Stack.cpp

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

I searched for it but could not solve the problem. I think something is wrong with my inclusion of order, or maybe I used pointers incorrectly.

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

Here is my make file:

 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 can I overcome this mistake? Any ideas?

+3
source share
1 answer

A stack is a template. The full definition should go in its header file. That is, do not split it into an .h and .cpp file.

+9
source

Source: https://habr.com/ru/post/908845/


All Articles