注册 登录
编程论坛 C++教室

为啥我在模板类中声明构造函数不定义会报LNK2019错误啊

陈紫文 发布于 2018-09-06 20:21, 1439 次点击
A.h
template<class T>
class A
{
public:
    A(){ a = 0; }
    void show()
    {
        cout << "a=" << a << endl;
    }
private:
    T a;
};
源.cpp
#include <iostream>
#include "A.h"
using namespace std;

template<template <typename T>class thing>
class clab
{
public:
    clab(){}
/*
    只声明构造函数,不定义如clab();
    会报错
    错误    1    error LNK2019: 无法解析的外部符号 "public: __thiscall clab<class A>::clab<class A>(void)"
    (??0?$clab@VA@@@@QAE@XZ),该符号在函数 _main 中被引用   
    模板类当初参数
    错误    2    error LNK1120: 1 个无法解析的外部命令   
   


    */
    void show(){ s1.show(); }
private:
    thing<int>s1;
    thing<double>s2;
};

int main()
{
    clab<A> s1;
    s1.show();
    //A<int>a;   
    //a.show();
    while (1);
    return 0;
}
2 回复
#2
rjsp2018-09-06 20:46
没看懂你想表达什么。
你没定义的话,编译器就会告诉你找不到定义。这难道有什么逻辑不通吗?
#3
Jonny02012018-09-07 10:10
这种属于链接错误
对函数体的定义一般叫做函式的实作
只定义不实作会产生链接错误
LNK 也就是 link 链接的意思
你用的应该是 VS
1