小编gim*_*iki的帖子

自定义类对象的 C++ 向量 - 删除复制构造函数 - std::ifstream

我正在尝试创建自定义类 A 的对象向量:

\n\n
class A {\n    std::ifstream _file;\n    std::string _fileName;\npublic:\n    A(std::string &fileName) : _fileName(fileName) {\n        this->_file = std::ifstream(this->_fileName, std::ios::in);\n    }\n    ~A() {\n        this->_file.close();\n    }\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n

主要是我使用 for 循环迭代文件名向量来推送 A 类的 n 个对象。

\n\n

例子:

\n\n
#include <iostream>\n#include <string>\n#include <vector>\n#include "A.hpp"\n\nint main() {\n\n    std::vector<A> AList;\n    std::vector<std::string> fileList = { "file1", "file2", "file3" };\n\n    for (auto &f : fileList) {\n        std::cout << f << std::endl;\n        A tempObj(f);\n        AList.emplace_back(tempObj);\n    }\n\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但我收到此错误:\n/usr/include/c++/9.1.0/bits/stl_construct.h:75:7: error: use of deleted function \xe2\x80\x98A::A(const A&)\xe2\x80\x99 …

c++ vector copy-constructor ifstream c++11

2
推荐指数
1
解决办法
1845
查看次数

标签 统计

c++ ×1

c++11 ×1

copy-constructor ×1

ifstream ×1

vector ×1