类在具有 std::map<int,std::ofstream> 成员时不会从 DLL 导出
class does not export from dll when having std::map<int,std::ofstream> member
考虑导出类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
,导出或不导出。
相关文章:
- std::ofstream 作为类成员删除复制构造函数?
- 使用 std::index_sequence 初始化具有固定大小数组成员的 POD 结构容器
- std::to_string - 'to_string' 不是 'std' 的成员 - Visual Studio Code 1.42.0
- 使用 std::async 时死锁,将来作为成员
- 可变参数模板参数扩展 类型为 std::function 的类成员
- FreeFileSync C++错误:'byte'不是 'std' 的成员
- 为什么 std::sort 找不到合适的(静态成员)函数重载?
- 对带有唯一指针的 std::thread 使用类成员函数时出现编译错误
- 在模板化成员函数的返回类型中使用 std::enable_if 时的编译器差异
- 有没有办法避免为 std::variant 类成员中的所有类型编写构造函数?
- 是否允许类类型的 std::function 成员变量(不完整类型)?
- 访问 std:vector 的类成员 std:vector 在一个类中与另一个 std:vector
- std::异步与非静态成员函数
- 如何在 c++11 中静态断言 std::array 类成员进行排序?
- 将初始化器列表/聚合初始化转发到 std::array 成员
- C++ IDE 不会推断/自动完成对模板类中的 std::array 下标表达式的成员访问
- 在哪里可以找到 std::bitset 的数据成员?
- 没有'str'成员在 GCC 和 Clang 'std::basic_ostream<char>',但 MSVC 没有问题
- unique_ptr问题:不是'std'成员
- std::成员函数的异步调用