Search code examples
c++c++11ifstream

ifstream attempting reference to a deleted function


I'm writing a code for a virtual tournament. The problem is that team class has an ifstream object, I understand that stream objects do not have copy constructors, therefore i converted playing8 from vector of team objects to pointer to object, So that team objects will not be copied. But now I get this error

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.0\vc\include\xmemory0 592 1   Assignment3

How do I resolve this without removing the ifstream object from the team class? Here's code for 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();
};

Code for the tournament constructor:

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);
    }
}

Code for select playing 8:

void Tournament::selectPlaying8(){
    for (int i = 0; i < 7; i++) {
        playing8.push_back(&teams[i]);
        playing8[i]->set_playing();
    }
}

Attributes of team class

#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;
};

I'm using visual studio express 2013.


Solution

  • This code

    playing8.push_back(&teams[i]);
    

    makes a copy of the team class instance pushed back using the compiler generated copy constructor. It tries simply to copy each member.

    ifstream doesn't provide a copy constructor (it's deleted), hence you get this error.

    To fix this you'll need to use a ifstream* pointer, or ifstream& reference.