gpt4 book ai didi

c++ - 将 ofstream 的实例插入到容器中

转载 作者:行者123 更新时间:2023-11-28 05:11:27 25 4
gpt4 key购买 nike

我创建了一个类 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

如果您的文件不可移动构造,您可以就地构造它:

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++ - 将 ofstream 的实例插入到容器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43429708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com