1.在文件1中定义了基类:#ifndef __MY_BASE_HPP__
#define __MY_BASE_HPP__namespace MyDll
{
class MyBase
{
public:
virtual ~MyBase() {};
virtual void Print() = 0;protected:
MyBase() {};
}; //class MyBase
}; //namespace MyDll#endif //#ifndef __MY_BASE_HPP__在文件2中定义并导出了派生类:
#ifndef __MY_DERIVE_HPP__
#define __MY_DERIVE_HPP__#include <string>
#include "MyBase.hpp"using namespace std;namespace MyDll
{
class __declspec(dllexport) MyDerive : public MyBase
{
public:
MyDerive(const string &sText);
virtual ~MyDerive();
virtual void Print();private:
string _sText;
MyDerive();
}; //class MyDerive
}; //namespace MyDll#endif //#ifndef __MY_DERIVE_HPP__编译会得到如下警告信息:
1.
warning C4275: non dll-interface class 'MyDll::MyBase' used as base for dll-interface class 'MyDll::MyDerive' mybase.hpp(7) : see declaration of 'MyBase'
2.
warning C4251: '_sText' : class 'std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' needs to have dll-interface to be used by clients of class 'MyDll::MyDerive'但是如果将文件1和文件2合并成一个文件,或者将class Base也导出,就不会有问题,请问该怎么消除这些警告信息,即导出带基类的C++类,但是不导出基类??

解决方案 »

  1.   

    不可能把?不导出基类的话,应用方通过 MyDerive 实例调用基类函数时 linker 怎么办?纯虚基类直接在头文件给出就可以了。如果需要 MyBase 不可见,用聚合代替派生
      

  2.   

    我测试过,不导出基类MyDerive是可以使用的,只是编译的时候有很多警告信息。因为基类所有方法的定义已经包含在头文件中了,即基类没有CPP文件,如果有是否可以还没有测试过。
      

  3.   

    in you MyDerive.cpp add such:
    namespace MyDll
    { MyDerive::MyDerive(const string &sText){}
    MyDerive::~MyDerive(){}
    void MyDerive::Print(){}
    MyDerive::MyDerive(){}
    }
      

  4.   

    还是不明白,在DLL中怎么使用stl才没有警告信息呀??