• C/C++ warning C4251: class ... 需要有 dll 接口由 class“..” 的客户端使用


    {

    在DLL编程中, 如果调用模版类, 则可能出现类似以下的错误:

    1>xclock.h(29): warning C4251: “XClock::m_FileName”: class“std::basic_string<char,std::char_traits<char>,std::allocator<char>>”需要有 dll 接口由 class“XClock”的客户端使用
    1>xstring(2633): note: 参见“std::basic_string<char,std::char_traits<char>,std::allocator<char>>”的声明
    1>xclock.h(30): warning C4251: “XClock::m_Stage”: class“std::queue<std::string,std::deque<_Ty,std::allocator<_Ty>>>”需要有 dll 接口由 class“XClock”的客户端使用
    1>          with
    1>          [
    1>              _Ty=std::string
    1>          ]

    模板类在绝大多数的编译器里要求实现一起提供,因此不能放在dll中

    Solution:


    第一种: 无视它或者#pragma warnind( disable: 4251 )
    第二种:将该数据改为指针方式:
    class __declspec( dllexport ) Test
    {
    public:
    std::vector<int>* m_objCon;
    };
    然后在构造函数和析构函数中分别初始化和释放它。
    第三种:
    将该模板类及其依赖类导出。
    #include <iostream>
    #include <vector>
    using namespace std;

    class __declspec( dllexport ) Test
    {
    public:
    template  class __declspec( dllexport ) std::allocator<int>;
    template  class __declspec( dllexport ) std::vector<int, std::allocator<int> >;
    public:

    std::vector<int> m_objCon;
    };

    int main()
    {
        return 0;
    }
    这 种方法要注意的是必须要把要导出模板类的模板数据成员一并导出。有点类似于显式实例化。比如说你要导出boost::shared_ptr就还必须将其依 赖的shared_count一并导出。导出map还需要导出对应pair等等。很麻烦啦~所以我们还是选择第四种吧。

    第四种:Impl。
    #include <iostream>
    #include <vector>
    using namespace std;

    // 这些放到.h中
    class Test_imp;
    class __declspec( dllexport ) Test
    {
        // 构造函数中初始化 析构中释放m_pImp;
        void test();

    public:
        Test_imp* m_pImp;
    };

    // 这个类放到cpp中去
    class  Test_imp
    {
    public:
        void test(){}
        std::vector<int> m_objCon;
    };


    // 放到cpp中
    void Test::test()
    {
    m_pImp->test();
    }

    }

  • 相关阅读:
    SDN组网相关解决方案
    Linux C中结构体初始化
    lambda函数、lambda表达式
    流量工程 traffic engineering (TE)
    BGP路由协议详解(完整篇)
    Overlay network 覆盖网络
    覆盖路由
    重叠(Overlapping) NAT
    Multiprotocol Label Switching (MPLS)
    MPLS
  • 原文地址:https://www.cnblogs.com/YZFHKMS-X/p/11834989.html
Copyright © 2020-2023  润新知