【问题标题】:Error LNK2019: unresolved external symbol error message when compiling program using classes错误 LNK2019:使用类编译程序时出现无法解析的外部符号错误消息
【发布时间】:2014-09-11 16:18:16
【问题描述】:

我这周刚刚安装了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;
}

谢谢

【问题讨论】:

标签: c++ visual-studio class linker


【解决方案1】:

模板实现必须在编译时可供编译器使用。必须在头文件中定义template&lt;typename T&gt; Stackt&lt;T&gt;::Stackt(int n)

【讨论】:

    猜你喜欢
    • 2020-07-09
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 2012-07-10
    • 1970-01-01
    • 2013-06-15
    • 2015-01-18
    相关资源
    最近更新 更多