将流的实例插入到容器
Insert instance of ofstream to container
我创建了一个类file
,它是std::ofstream
的包装器。我创建了一个Container
来包含file
的所有实例。
class file
{
private:
std::ofstream ofs;
public:
void open(std::string filename);
void write(std::string s);
void close();
};
class Container
{
private:
std::map<int, file> m;
public:
void insert(file f,int i)
{
m.insert(std::pair<int,file> (i,f));
}
void get(int i)
{
m.at(i);
}
};
但是,此代码中存在一个问题。在insert
方法中,我正在尝试复制一个无法完成std::pair<int,file>
,因为std::ofstream
的复制构造函数被删除(请参阅下面的编译错误)。
我想在容器中连续添加file
实例。我该怎么做?
这是编译错误
In file included from src/test.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:627:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:274:23: error: call to implicitly-deleted copy constructor of
'file'
: first(__x), second(__y) {}
^ ~~~
src/test.cpp:35:22: note: in instantiation of member function 'std::__1::pair<int, file>::pair' requested here
m.insert(std::pair<int,file> (i,f));
^
src/test.cpp:9:21: note: copy constructor of 'file' is implicitly deleted because field 'ofs' has a deleted copy constructor
std::ofstream ofs;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/fstream:1168:5: note: copy constructor is implicitly deleted because
'basic_ofstream<char, std::__1::char_traits<char> >' has a user-declared move constructor
basic_ofstream(basic_ofstream&& __rhs);
^
1 error generated.
如果你想用file
初始化你的Container
而不复制它们,那么你可以依靠std::ofstream
的移动构造函数:
void insert(file &&f, int i)
{
m.insert(std::pair<int,file>(i, std::move(f)));
}
然后,您将执行以下操作:
cont.insert(file{}, 0); // Construct a file when inserting, you already have an rvalue
或:
file myfile;
myfile.open(...); // Do whatever you want with myfile...
cont.insert(std::move(myfile), 0); // And then move it, as long as you are not using if after
如果您的file
不可移动构造,则可以就地构造它:
template <typename... Args>
void insert(int i, Args&&... args) {
m.emplace(std::piecewise_construct, std::tuple<int>(i),
std::tuple<Args>(std::forward<Args>(args)...));
}
此外,如果要插入默认初始化的文件(如第一个示例所示),只需执行以下操作:
void insert(int i)
{
m[i];
}
相关文章:
- 从C++实例化QML
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 如何在c++中为模板函数实例创建快捷方式
- 预处理器:插入结构名称中的前一个行号
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 在未初始化映射的情况下,将值插入到映射的映射中
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 如何在c++中只将键插入到bimap的一侧
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 如何将结构插入到集合中并打印集合的成员
- C++json插入数组
- 将流的实例插入到容器
- 将unordered_map的新实例插入到unordered_map中
- 插入std :: basic_ostream实例化输出时的内存故障
- 将类的实例插入到vector中