Search code examples
c++referencefstreamifstream

How do you properly use a reference of ifstream in this context? Error C2248


I'm using an ifstream to grab input from a file as seen in my code below.

I realize that you cant grab an ifstream directly as it is a private member. What is the work around to grabbing it via reference and what would the syntax look like?

Below is my B.cpp and B.h, I don't think attaching the other files are necessary, but can if need be. Also I've attached the error message beneath that!

Thanks for any help in advance!

B.h:

#ifndef B_H
#define B_H
#include <string> 
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <iostream>

class B {
    public:
        //Variable declarations
        std::string line, delimiter, token, str;
        std::ifstream inputfile;
        size_t pos;
        int lineRead;

        //Function declarations
        B();
        void setValues(int lineNum);
        std::string printValues();
        bool to_bool(std::string str);
    protected:
        float f;
        int i;
        bool b;
        std::string s;
};

#endif

B.cpp:

#include "B.h"

B::B() {

}

void B::setValues(int lineNum) {
    lineRead = 0;
    pos = 0;
    delimiter = " ";

    inputfile.open("file.txt");

    while (!inputfile.eof()) {
        //increment line counter
        lineRead++;

        //read line
        getline(inputfile,line);

        //if this is the line requested fill the template members with the
        //correct data. <string, int, float, bool>
        if(lineNum == lineRead){
            //getting the string
            pos = line.find(delimiter);
            token = line.substr(0, pos);
            str = token;
            line.erase(0, pos + delimiter.length());

            //getting the integer
            pos = line.find(delimiter);
            token = line.substr(0, pos);
            i = stoi(token);
            line.erase(0, pos + delimiter.length());

            //getting the float
            pos = line.find(delimiter);
            token = line.substr(0, pos);
            f = stof(token);
            line.erase(0, pos + delimiter.length());

            //getting the boolean
            pos = line.find(delimiter);
            token = line.substr(0, pos);
            b = to_bool(token);
            line.erase(0, pos + delimiter.length());
        }
    }

    //close the file
    inputfile.close();
}

std::string B::printValues() {
    return s + " " + std::to_string(i) + " " + std::to_string(f) + " " + std::to_string(b) + "\n";
}

//Changes a string to lower case and then reads the string
//to find if the value is true or false. Returns a boolean.
bool to_bool(std::string str) {
    transform(str.begin(), str.end(), str.begin(), ::tolower);
    std::istringstream is(str);
    bool tempB;
    is >> std::boolalpha >> tempB;
    return tempB;
}

S.h:

#ifndef S_H
#define S_H
#include "B.h" // required to make B known here
#include <string> // known through B, but better safe than sorry
class S : public B {

public:
    S(std::string name);
    std::string subPrint(); // ***
protected:
    std::string s2;
};
#endif

S.cpp:

#include "S.h"

S::S(std::string name) : s2(name) {

}

std::string S::subPrint () {
    return s2 + " " + printValues();
}

Main.cpp:

#include "S.h"
#include <vector>

using namespace std;

int main() {
    int itPos = 0;

    vector<S> v;

    S s1("Jon");
    S s2("Mike");
    S s3("Kim");
    S s4("Steve");
    S s5("Kevin");

    v.push_back(s1);
    v.push_back(s2);
    v.push_back(s3);
    v.push_back(s4);
    v.push_back(s5);

    cout << v[0].subPrint();

    system("pause");
    return 0;
};

Error


Solution

  • You issue here is that std::ifstream is not copyable. That meanse B cannot be copied and in turn S cannot be copied. When you add the S's into the vector

    v.push_back(s1);
    v.push_back(s2);
    v.push_back(s3);
    v.push_back(s4);
    v.push_back(s5);
    

    the vector tries to copy what was passed to it to place it in the vector.

    A simple fix for this is to drop the ifstream from the class members and only have it as a local variable in setValues since that is the only place it is used.