编译器警告(等级 1)C4251Compiler Warning (level 1) C4251

identifier: 类 type 需要有 dll 接口的类 type2 的客户端使用'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'

若要导出具有的类时,数据损坏的可能性降至最低__declspec (dllexport),,请确保:To minimize the possibility of data corruption when exporting a class with __declspec(dllexport), ensure that:

  • 所有静态数据是通过从 DLL 导出的函数的访问。All your static data is access through functions that are exported from the DLL.

  • 您的类没有内联的方法可以修改静态数据。No inlined methods of your class can modify static data.

  • 您的类没有内联的方法使用的 CRT 函数或其他库函数使用静态数据 (请参阅潜在错误对象跨 DLL 边界传递 CRT有关详细信息)。No inlined methods of your class use CRT functions or other library functions use static data (see Potential Errors Passing CRT Objects Across DLL Boundaries for more information).

  • 您的类没有方法 (而不考虑内联) 可以使用类型其中 EXE 和 DLL 中的实例化具有静态数据的差异。No methods of your class (regardless of inlining) can use types where the instantiation in the EXE and DLL have static data differences.

您可以避免通过定义一个 DLL,它定义了具有虚函数的类和函数,可以调用来实例化并删除对象类型的导出类。You can avoid exporting classes by defining a DLL that defines a class with virtual functions, and functions you can call to instantiate and delete objects of the type. 然后,可以只需调用虚函数的类型。You can then just call virtual functions on the type.

如果派生的类型,则可以忽略 C4251C++标准库,编译调试版本 (/MTd),其中编译器错误消息是指 _Container_base。C4251 can be ignored if you are deriving from a type in the C++ Standard Library, compiling a debug release (/MTd) and where the compiler error message refers to _Container_base.

// C4251.cpp
// compile with: /EHsc /MTd /W2 /c
#include <vector>
using namespace std;
class Node;
class __declspec(dllimport) VecWrapper : vector<Node *> {};   // C4251