相关疑难解决方法(0)

为什么模板只能在头文件中实现?

引自C++标准库:教程和手册:

目前使用模板的唯一可移植方法是使用内联函数在头文件中实现它们.

为什么是这样?

(澄清:头文件不是唯一的便携式解决方案.但它们是最方便的便携式解决方案.)

c++ templates c++-faq

1660
推荐指数
14
解决办法
46万
查看次数

未解析的外部符号“公共:__thiscall Vector<int> [...]”

因此,我创建了一个基本的 VC++ 程序,并创建了一个具有 1 个方法的模板类(除了构造函数和析构函数)。我收到以下错误:

>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Vector<int>::~Vector<int>(void)" (??1?$Vector@H@@QAE@XZ) referenced in function _main
>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Vector<int>::Vector<int>(int)" (??0?$Vector@H@@QAE@H@Z) referenced in function _main
>c:\users\edy\documents\visual studio 2010\Projects\ex01\Debug\ex01.exe : fatal error LNK1120: 2 unresolved externals
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

(CPP 类文件)

using namespace std;
#include "Vector.h"
template <class T> Vector<T>::Vector(int n)
{
    this.crt = 0;
    this.dim = n;
    this.elem = new T[100];
}

template <class T> void Vector<T>::add(T e)
{
    this.elem[crt] = e; …
Run Code Online (Sandbox Code Playgroud)

c++ linker visual-c++

0
推荐指数
1
解决办法
3533
查看次数

标签 统计

c++ ×2

c++-faq ×1

linker ×1

templates ×1

visual-c++ ×1