考虑导出类 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 接口?我之前在 dll 中将 std::map 用于除 std::ofstream 之外的对象,没有任何问题。请让我知道我错过了什么......
PS TEST_API 很简单
#ifdef TEST_EXPORTS
#define TEST_API __declspec(dllexport)
#else
#define TEST_API __declspec(dllimport)
#endif