如果流试图引用一个已删除的函数
ifstream attempting reference to a deleted function
我正在为虚拟锦标赛编写代码。问题是团队类有一个ifstream对象,我理解流对象没有复制构造函数,因此我将playing8从团队对象的向量转换为指向对象的指针,这样团队对象就不会被复制。但是现在我得到了这个错误
Error 16 error C2280: 'std::basic_ifstream<char,std::char_traits<char>>::basic_ifstream(const std::basic_ifstream<char,std::char_traits<char>> &)' : attempting to reference a deleted function c:program files (x86)microsoft visual studio 12.0vcincludexmemory0 592 1 Assignment3
如何解决这个问题而不从团队类中删除ifstream对象?下面是tournament.h
的代码#include "team.h"
class Tournament
{
std::ofstream out_file;
std::ifstream in_file;
std::vector<team> teams;
std::vector<team*> playing8;
public:
void schedule();
void schedule2();
void tfinal();
void selectPlaying8();
void rankTeams();
void match(int,int);
Tournament();
~Tournament();
};
锦标赛构造函数代码:
Tournament::Tournament()
{
srand(time(NULL));
in_file.open("team_list.txt");
string input;
int noteam=0;
while (getline(in_file, input)){
noteam++;
}
in_file.close();
for (int i = 0; i < noteam;i++){
string x=to_string(i)+".csv";
team temp(x);
temp.set_teamform((6 + rand() % 5) / 10.0);
teams.push_back(temp);
}
}
选择播放8的代码:
void Tournament::selectPlaying8(){
for (int i = 0; i < 7; i++) {
playing8.push_back(&teams[i]);
playing8[i]->set_playing();
}
}
团队类属性
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include "Player.h"
class team
{
private:
std::ifstream in_file;
std::vector<Player> playing11;
std::string teamname;
std::vector<Player> player;
bool playing;
float matchperformance;
float teamform;
float team_rank_score;
};
我用的是visual studio express 2013
此代码
playing8.push_back(&teams[i]);
使用编译器生成的复制构造函数生成team
类实例的副本。它只是简单地复制每个成员。
ifstream
没有提供复制构造函数(它被删除了),因此你会得到这个错误。
要解决这个问题,你需要使用ifstream*
指针,或ifstream&
引用。
不是每个变量都必须是类变量。一般来说,变量应该保存在尽可能小的范围内。
保持你的文件作为本地变量,没有必要把它们作为类字段
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 从链接列表c++中删除一个项目
- 删除映射和分割错误中的一个过去结束元素
- 使用另一个字符串从字符串中删除空格
- 编写一个函数来删除单链表中的节点(尾部除外),仅授予对该节点的访问权限
- 创建了一个链表,但如何删除 c++ 中的"所有"节点
- 如何知道地图中的最后一个元素是否被删除?
- G++ 发出警告,要求删除一个代码的数组,但不删除另一个代码的数组
- 动态分配列表 - 创建一个函数,用于删除所有包含偶数值的元素
- 如何删除矢量中的重复值,最后一个除外
- C++ 删除存在于另一个矢量中的矢量项,同时保留顺序
- 当类型适当的构造函数可用时,为什么一个编译器尝试使用已删除的副本构造函数
- 在 c++ 中打开文件、删除标点符号并追加到另一个文件中
- 删除链接列表中剩余的最后一个节点
- 编译器在C++中调用另一个函数时,在参数中查找已删除的构造函数
- 避免在使用链接列表从 deque 中删除最后一个节点时出现内存泄漏
- 在C++中为二叉搜索树创建一个删除函数
- 当编译一个删除停止字并应用大小写折叠的程序时,会出现一个奇怪的代码
- 为什么C++仍然有一个删除[]和一个删除运算符
- 一个删除行的函数