0

I just installed VS 2013 this week (Have been using 2012 version before). I got an error in this first program I am trying to do using classes. I tried to read other threads with similar error but none of the answers have worked for me so far. This is supposed to be a program that is based on a stack.

Error

1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1>  Source.cpp
1>  Generating Code...
1>  Compiling...
1>  Stackt.cpp
1>  Generating Code...
1>Source.obj : error LNK2019: unresolved external symbol "public: __thiscall Stackt<char>::Stackt<char>(int)" (??0?$Stackt@D@@QAE@H@Z) referenced in function _main
1>C:\Users\Documents\Visual Studio 2013\Projects\Project1\Debug\Project1.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

stackt.h

#ifndef STACKT_H           
#define STACKT_H       
template <class Type>
class Stackt
{
public:
    Stackt(int n = 128);
private:
    Type *stack;
    int top, max;
};
#endif

stack.cpp

#include "Stackt.h"
template <class Type>
Stackt<Type>::Stackt(int n)
{
    max = n;
    stack = new Type[max];
    top = -1;
}

source.cpp

#include <iostream>
#include "Stackt.h"

void main()
{
    Stackt<char> s1;
}

Thanks

  • possible duplicate of [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – IdeaHat Sep 11 '14 at 16:30

1 Answers1

0

Template implementations must be available to the compiler at compile time. You must define template<typename T> Stackt<T>::Stackt(int n) in the header file.

IdeaHat
  • 7,641
  • 1
  • 22
  • 53