zoukankan      html  css  js  c++  java
  • 模板类中方法的声明与定义 unresolved external symbol

    将模板类的声明放在.h中, 实现放在.cpp中结果出现unresolved external symbol;

    解决办法:

    • 包含编译模式:

    1.将实现一起放在.h中

    • 分离编译模式

    2.在.h中使用export(编译器要支持, 很多貌似不支持)

    // ----- Queue.h -----
    // 声明 Queue 是一个可导出的 (exported) 类模板
    export template <class Type>
    class Queue {
     // ...
    public:
     Type& remove();
     void add( const Type & );
     // ....
    };

    3.在.cpp中加入你想实例化的类型的显式声明,你必须提前知道那种类型的实例化是你想要的(Another option is to put the code in the cpp file and in the same cpp file add explicit instantiations of the template with the types you expect to be using. This is useful if you know you're only going to be using it for a couple of types you know in advan//ce.)

    // 显式实例声明
    template class Queue<int>;

    4.在.h中包含.cpp:在.h后面#include“Queue.cpp”(但是我试的时候, 有错误提示重复定义,不知道怎么弄)maybe add inline

  • 相关阅读:
    爬虫基础 2.1 http原理
    爬虫基础 2.1 http原理
    3.29上午
    3.28
    3.27下午
    3.27上午
    3.24上午
    3.23下午
    3.23上午
    3.22上午
  • 原文地址:https://www.cnblogs.com/lidan/p/2239521.html
Copyright © 2011-2022 走看看