main.cpp
Код: Выделить всё
#include
#include "Stack.h"
using namespace std;
int main(int argc,char* argv[])
{
Stack st;
st.push(1);
return 0;
}
Код: Выделить всё
#ifndef _STACK_H
#define _STACK_H
template
class Stack
{
private:
struct Node
{
Node* _prev;
T _data;
Node* _next;
};
int _size;
Node* _pos;
public:
Stack();
T pop();
void push(T const &el);
int getSize() const;
};
#endif
Код: Выделить всё
#include "Stack.h"
#include
template
Stack::Stack()
{
_size = 0;
_pos = (Node*)malloc(sizeof(Node));
_pos->_prev = NULL;
_pos->_next = NULL;
}
template
T Stack::pop()
{
if (_size == 0)
return NULL;
T tmp = _pos->_data;
if (_pos->_prev == NULL)
free(_pos);
else
{
_pos->_prev->_next = _pos->_next;
if (_pos->_next != NULL)
{
_pos->_next->_prev = _pos->_prev;
}
free(_pos);
}
_size--;
return tmp;
}
template
void Stack::push(T const &el)
{
Node* n = (Node*)malloc(sizeof(Node));
_pos->_next = n;
n->_prev = _pos;
n->_data = *el;
_pos = n;
_size ++;
}
template
int Stack::getSize() const {return _size;};
Код: Выделить всё
ccyDhLTv.o:main.cpp:(.text+0x16): undefin
ed reference to `Stack::Stack()'
ccyDhLTv.o:main.cpp:(.text+0x32): undefin
ed reference to `Stack::push(int const&)'
collect2: ld returned 1 exit status
ОС — Windows
строка компиляции — g++ main.cpp Stack.h Stack.hpp -o main.exe
Подробнее здесь: https://stackoverflow.com/questions/919 ... ompilation