zoukankan      html  css  js  c++  java
  • (转)如何解决VC中的警告warning C4251 needs to have dll-interface

    这通常是由于以数据成员方式在DLL导出类中使用了模板类造成的。比如:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class __declspec( dllexport ) Test
    {
    public:
    std::vector<int> m_objCon;
    };
    
    int main()
    {
    
    
    return 0;
    }
    
    这会导致这个警告:
    warning C4251: “Test::m_objCon”: class“std::vector<_Ty>”需要有 dll 接口由 class“Test”的客户端使用 
    1>        with
    1>        [
    1>            _Ty=int
    1>        ]
    这个问题主要要描述的是不同的地方的vector的实现可能不一样所造成的问题。所以我们应该将其导出。有很多方法可以解决这个问题的。
    
    第一种: 无视它或者#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();
    }
    
    int main()
    {
    return 0;
    }
    

      

  • 相关阅读:
    (4.38)sql server中的事务控制及try cache错误处理
    (4.37)sql server中的系统函数
    【3.5】mysql常用开发规则30条
    Linux学习笔记(17)Linux防火墙配置详解
    (5.16)mysql高可用系列——keepalived+mysql双主ha
    mysql online DDL
    (5.3.8)sql server升级打补丁
    python request
    python 模块被引用多次但是里面的全局表达式总共只会执行一次
    Protocol Buffer Basics: Python
  • 原文地址:https://www.cnblogs.com/gushandujian/p/3651731.html
Copyright © 2011-2022 走看看