SarKerson 2016-07-02 06:35 采纳率: 50%
浏览 1627
已采纳

c++类模板,调用结构体模板出错!!

#ifndef _LIST_H_
#define _LIST_H_
template <typename T>
struct Num {
    T num;
    Num *next;
};
template <typename T>
class List {
private:
    Num<T>* pHead;
public:
    List();
    int count();
    void create(T *a, int n);
    void list_();
    void add(T key, int n);
    void Swapat(int a, int b);
    void Swapnum(T num1, T num2);
    int& at(int n);
    void Sort();
    void del(int n);
    void delTheSame();

};
#endif

图片说明
Severity Code Description Project File Line
Error LNK1120 3 unresolved externals Test C:\Users\Sar.Kerson\Desktop\Test1\Test\Debug\Test.exe 1
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol "public: __thiscall List::List(void)" (??0?$List@H@@QAE@XZ) referenced in function main Test C:\Users\Sar.Kerson\Desktop\Test1\Test\Test\main.obj 1
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol "public: void __thiscall List::create(int *,int)" (?create@?$List@H@@QAEXPAHH@Z) referenced in function _main Test C:\Users\Sar.Kerson\Desktop\Test1\Test\Test\main.obj 1
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol "public: void __thiscall List::list
(void)" (?list_@?$List@H@@QAEXXZ) referenced in function _main Test C:\Users\Sar.Kerson\Desktop\Test1\Test\Test\main.obj 1

求助大侠这是怎么回事?!(以下为几个报错函数的实现)

template <typename T>
List<T>::List() {
    pHead = NULL;
}
template <typename T>
void List<T>::create(T *a, int n) {
    Num<T> *pTail = NULL;
    for (int i = 0; i < n; ++i) {
        Num<T> *t = new Num<T>;
        t->num = a[i];
        t->next = NULL;
        if (pHead == NULL) {
            pHead = t;
            pTail = pHead;
        }
        else {
            pTail->next = t;
            pTail = t;
        }
    }
}
template <typename T>
void List<T>::list_() {
    Num<T> *t = pHead;
    while (t != NULL) {
        cout << t->num << " ";
        t = t->next;
    }
    cout << endl;
}



  • 写回答

1条回答 默认 最新

  • Valtava 2016-07-02 13:30
    关注

    把实现和类定义放在同一个文件里(#include实现或者直接写在一个文件里),模板和内联一样,分开放的话链接的时候模板没法实例化

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥150 求 《小魔指》街机游戏机整合模拟软件