c++ - 当具有 std::map<int,std::ofstream> 成员时,类不从 dll 导出

标签 c++ dll dictionary fstream

考虑导出类 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.0\VC\include\fstream(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.0\VC\include\ios(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接口(interface)?我之前在 dll 中使用 std::map 来处理 std::ofstream 以外的对象,没有任何问题。请让我知道我错过了什么......

附言TEST_API 很简单

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

最佳答案

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

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

关于c++ - 当具有 std::map<int,std::ofstream> 成员时,类不从 dll 导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17755572/

相关文章:

c++ - 何时检查项目是否已存在于 C++ 集合/映射中?

c++ - 为什么 Windows Defender 扫描我的代码?

c++ - 从 C++ 调用 matlab fmincon

scala - Scala 中对映射值进行求和和分组

c++ - 当通过 const 引用传递的对象被添加到像 std::map 这样的容器时会发生什么?

c++ - 使用片段着色器计算纹素

.net - IronPython -> .exe 可执行并与所有 DLL 一起分发帮助

windows - 找不到函数 dllexport

c++ - 在单独的项目中调试多个相关的 dll

python - 比较两个字典列表并输出差异