TheTobruk February 2016

Deprecated getch() - switching to recommended by compiler _getch() still causes problems

I would really need your help on this one and I hope I'll get some clue about my compiler error.

The problematic piece of my code:

char user_ch;
do {
        user_ch=_getch(); // i changed it from getch() to fix the previous error, didn't help it seems
        switch (user_ch) {
            case '1': response='1'; break;
            case '3': response='3';
        }
} while (response!='1'||response!='3');

Compiler error (what's peculiar it's 'build' error instead of 'debug' error):

error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

Compiler refers to this piece of code in fstream file:

private:
_Myfb _Filebuffer;  // the file buffer
};

As someone pointed out I'm posting my usage of fstream functions and objects. My functions parsing settings.txt file

bool read_bool(ifstream file, int verse) {
string temp_var;
for (int i=0; i<verse; i++)
    getline(file, temp_var);
if (temp_var=="true") return true;
else return false; }

int read_int(ifstream file, int verse) {
string temp_var;
for (int i=0; i<verse; i++)
    getline(file, temp_var);
return stoi(temp_var); }

t_direction read_t_dir(ifstream file, int verse) {
char temp_var;
for (int i=0; i<verse; i++)
file.get(temp_var);
t_direction pre_return;
switch (temp_var) {
    case '1': pre_return=to_mother; break;
    case '2': pre_return=to_foreign; break;
    case '3': pre_return=rndom;
}
return pre_return; }

string read_string(ifstream file, int verse) {
string temp_var;
for (int i=0; i<verse; i++)
    getline(file, temp_var);
return temp_var; }

size_t lines_count(ifstream if_file) {
size_t a=0;
string temp_str;
while (getline(if_file, temp_str)) 
    ++a;
return a; }

bool add_mistake(ofstream file, string mistake, string to_trans, string target) {
file << "Ori        

Answers


templatetypedef February 2016

Your issue is in this part of the code:

string read_string(ifstream file, int verse) {
    string temp_var;
    for (int i=0; i<verse; i++)
        getline(file, temp_var);
    return temp_var;
}

size_t lines_count(ifstream if_file) {
    size_t a=0;
    string temp_str;
    while (getline(if_file, temp_str)) 
        ++a;
    return a;
}

Both of these functions take as input a parameter of type ifstream. Since you're not taking in the ifstream by reference, when you call this function, C++ will try to initialize if_file to a copy of the ifstream that you passed in as a parameter. This isn't allowed, since ifstreams can't be copied. Unfortunately, you tend to get garbage error messages like the one you showed above instead of something more useful in this case.

To fix this, change your code so that you take the parameters by reference:

string read_string(ifstream& file, int verse) {
    string temp_var;
    for (int i=0; i<verse; i++)
        getline(file, temp_var);
    return temp_var;
}

size_t lines_count(ifstream& if_file) {
    size_t a=0;
    string temp_str;
    while (getline(if_file, temp_str)) 
        ++a;
    return a;
}

That said, there are likely other errors in your code. I would strongly recommend commenting your code as you go and testing pieces incrementally, since as written the code is pretty tough to follow.

Post Status

Asked in February 2016
Viewed 2,458 times
Voted 5
Answered 1 times

Search




Leave an answer