类在具有 std::map<int,std::ofstream> 成员时不会从 DLL 导出

class does not export from dll when having std::map<int,std::ofstream> member

本文关键字:std 成员 DLL gt 导出 int map lt ofstream      更新时间:2023-10-16

考虑导出类streamTest的dll。以下代码:

class  streamTest
{
public:
    TEST_API streamTest();
    TEST_API ~streamTest();
private:
    std::map<int,std::ofstream> streamMap;
};

编译时没有错误,并且从链接到dll的应用程序运行良好,但是以下代码:

class TEST_API streamTest
{
public:
    streamTest();
    streamTest();
private:
    std::map<int,std::ofstream> streamMap;
};

发出警告然后出错:

1>warning C4251: 'streamTest::streamMap' : class 'std::map<_Kty,_Ty>' needs to have dll-interface to be used by clients of class 'streamTest'
1>          with
1>          [
1>              _Kty=int,
1>              _Ty=std::ofstream
1>          ]
1>c:Program Files (x86)Microsoft Visual Studio 10.0VCincludefstream(1116): error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:Program Files (x86)Microsoft Visual Studio 10.0VCincludeios(176) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          This diagnostic occurred in the compiler generated function 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream(const std::basic_ofstream<_Elem,_Traits> &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]

为什么它不编译,如何为std::map提供dll接口?我以前在dll中使用过std::map以外的对象,而没有遇到任何问题。请让我知道我错过了什么。。。

p.S.TEST_API只是

#ifdef TEST_EXPORTS
#define TEST_API __declspec(dllexport)
#else
#define TEST_API __declspec(dllimport)
#endif

ofstream不可复制。导出类会强制实例化map<int,std::ofstream>的所有方法,包括那些试图复制值的方法。

您使用的是VC10,它不支持C++11功能。我不相信你可以在那里的map中存储ofstream,导出或不导出。