考虑导出类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接口?我之前使用std :: map作为dll中除了std :: ofstream之外的对象而没有任何问题。请让我知道我错过了什么...
P.S。 TEST_API只是
#ifdef TEST_EXPORTS
#define TEST_API __declspec(dllexport)
#else
#define TEST_API __declspec(dllimport)
#endif
答案 0 :(得分:1)
ofstream
不可复制。导出类会强制实例化map<int,std::ofstream>
的所有方法 - 包括那些尝试复制值的方法。
您正在使用VC10,它不支持C ++ 11功能。我认为您不能将ofstream
存储在map
,导出或不导出。