C++ 实例化模板类时获取链接器错误

C++ 实例化模板类时获取链接器错误,c++,templates,visual-c++,C++,Templates,Visual C++,在main中实例化类时,我遇到以下两个错误: error LNK2019: unresolved external symbol "public: __thiscall templateClass<int>::templateClass<int>(void)" (??0?$templateClass@H@@QAE@XZ) referenced in function _wmain error LNK2019: unresolved external symbol "pu

main
中实例化
类时,我遇到以下两个错误:

error LNK2019: unresolved external symbol "public: __thiscall templateClass<int>::templateClass<int>(void)" (??0?$templateClass@H@@QAE@XZ) referenced in function _wmain

error LNK2019: unresolved external symbol "public: __thiscall templateClass<int>::~templateClass<int>(void)" (??1?$templateClass@H@@QAE@XZ) referenced in function _wmain
templateclass.c

如何解决此问题?

诸如
templateClass
之类的模板类必须(通常)在头文件(
.h
)中实现

因此,您需要将
templateclass.c
的内容移动到
templateclass.h
并删除
templateclass.c


有关更多信息,请参阅。

但在尝试打印数组元素时,我遇到另一个错误。错误C2275:“T”:非法使用此类型作为(int i=0;i@Maddyfire很高兴能帮助你。你的下一个问题是<代码> T <代码>是类型,<代码> ARR 是变量。你可能是指<代码>(int i=0;i#pragma once #define MAX 10 template<class T> class templateClass { private: T arr[MAX]; int size; public: templateClass(void); void initArrayWithZero(); void pushElementIntoArray(T element ); void printArray(); ~templateClass(void); };
#include "templateClass.h"

template <class T>
templateClass<T>::templateClass(void)
{
    size = 0;
}

template <class T>
templateClass<T>::~templateClass(void)
{
}

template <class T>
void templateClass<T>::pushElementIntoArray(T element )
{
    if(size<MAX)
    {
        T[size++] = element;
    }
    else
    {
        //Give error code
    }
}

template <class T>
void templateClass<T>::initArrayWithZero()
{
    for(i= 0; i<MAX; i++)
        T[i] = 0;
}

template <class T>
void templateClass<T>::printArray()
{
    for( int i =0; i<MAX; i++)
        cout<<"\n"T[i];
}
#include "stdafx.h"
#include "templateClass.h"

int _tmain(int argc, _TCHAR* argv[])
{
    templateClass<int> intarray;
    return 0;
}