考虑导出类 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。我不相信你可以存储 ofstream
在map
在那里,导出或不导出。
关于c++ - 当具有 std::map<int,std::ofstream> 成员时,类不从 dll 导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17755572/