错误LNK2019:使用类编译程序时未解决的外部符号错误消息

时间:2014-09-11 16:18:17

标签: c++ visual-studio class linker

我本周刚安装了VS 2013(之前一直使用2012版)。我在使用类尝试的第一个程序中遇到错误。我试图阅读其他线程有类似的错误,但到目前为止,没有一个答案对我有用。这应该是一个基于堆栈的程序。

错误

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;
}

由于

1 个答案:

答案 0 :(得分:0)

编译时编译器必须可以使用模板实现。您必须在头文件中定义template<typename T> Stackt<T>::Stackt(int n)