0

i started studying informatics about half a year ago and we are learning as a programming language, so im really new to coding. tried implementing a template for a stack today, however keeps telling me ": unresolved external symbol " when trying to compile, so there must be a (probably extremely dumb) error somewhere in my code. heres the error-message(parts of it in german, words should be easy to guess though):

1>main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public:        __thiscall stack<int>::stack<int>(void)" (??0?$stack@H@@QAE@XZ)" in Funktion "_main".
1>main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall stack<int>::~stack<int>(void)" (??1?$stack@H@@QAE@XZ)" in Funktion "_main".
1>main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: void __thiscall stack<int>::pop(void)" (?pop@?$stack@H@@QAEXXZ)" in Funktion "_main".
1>main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: void __thiscall stack<int>::push(int)" (?push@?$stack@H@@QAEXH@Z)" in Funktion "_main".

here the code: //stack.h

#pragma once

template <class obj>
class stack
{ 
int maxSize;
int currentSize;
obj * thisStack;

public:
stack(int size);
~stack();

bool isFull();
bool isEmpty();
obj top();
void pop();
void push(obj objekt);
};


//stack.cpp
#include "stdafx.h"
#include "stack.h"
#include <iostream>
using namespace std;


template <class obj> 
stack<obj>::stack(int size)
{
currentSize = 0;
maxSize = size;
thisStack = new obj[maxSize];
} 

template <class obj>
stack<obj>::~stack()
{
delete thisStack[];
}

template <class obj>
bool stack<obj>::isEmpty()
{
if (currentSize == 0)
    return true;
else
    return false;
}

template <class obj>
bool stack<obj>::isFull()
{
if (currentSize == maxSize)
    return true;
else
    return false;
}

template <class obj>
obj stack<obj>::top()
{
if (!isEmpty())
{
    return thisStack[currentSize];
}
else
{
    cout << "Stack is empty" << endl;
}

}

template <class obj>
void stack<obj>::push(obj objekt)
{
if (!isFull())
{
    thisStack[currentSize] = objekt;
    cout << "Object " << thisStack[currentSize] << "pushed on the stack" << endl;
    currentSize++;
}
else
{
    cout << "Der Stack is full" << endl;
}
}

template <class obj>
void stack<obj>::pop()
{
if (!isEmpty())
{
    cout << "The Object " << thisStack[currentSize - 1] << endl;
    currentSize--;
}
else
{
    cout << "Stack is empty" << endl;
}
}

...and the main.ccp where i tested it with a couple of integer values and found it not working

#include "stdafx.h"
#include "stack.h"

void main()
{
stack<int> myStack(10);
myStack.push(1);
myStack.push(2);
myStack.push(3);
myStack.push(4);
myStack.push(5);
myStack.push(6);
myStack.push(7);
myStack.push(8);
myStack.push(9);
myStack.push(10);
myStack.push(11);
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
getchar();
}

help would me much appreciated, tried finding the mistake for over an hour now, thanks

localhost
  • 375
  • 1
  • 3
  • 15
pR1sm
  • 1

1 Answers1

3

Definitions of class template member functions must reside in your header file, so that they are available in every translation unit that refers to them.

Your book should mention this.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055