1

every time I write loadChar(charVariable,positonOfCharacter) with this code:

bool LoadEntity::loadChar(char * outputChar,int position)
    {
        ifstream file(nameOfFile.c_str());

        if(!(file.good()))
            return false;

        file.seekg(position);
        if(file.get())
        {
            * outputChar = file.get();
            return true;
        }
        else
            return false;
    }`

I get this error: invalid conversion from 'char' to 'char* The code is supposed to return bool if the function was ran correctly and change the value of char outputChar to character in file on int position. What causes this problem?

Christophe
  • 68,716
  • 7
  • 72
  • 138
Gabrysiek Hej
  • 21
  • 1
  • 5

1 Answers1

1

Problem:

char charVariable;
...
loadChar(charVariable, positonOfCharacter); 

Here you are trying to pass a char value instead of the pointer (i.e. char*) that is expected by the function. This is illegal.

Easy solution:

Use the address of your variable when you call the function:

loadChar(&charVariable, positonOfCharacter);    // note the &

Alternative:

If you're not so familiar with pointers you could also change the function's signature and use a reference instead of a pointer. References allow you to change the value of the original variable:

bool LoadEntity::loadChar(char& outputChar, int position)  // note the &
{
    ... 
        outputChar = file.get();      // note: no * anymore
    ...
}

Unrelated issue:

There's a problem with your use of get(). The following will lead you to read twice the file but ignore the first input:

    if(file.get())
    {
        * outputChar = file.get();
        ...
    }

In addition, if no char is available the if might nevertheless be executed and return ture, as there is no guarantee that the function will return 0.

Prefer :

    if(file.get(*outputChar))
    {
        return true;
    }

Don't worry: the output char will not be changed if no char can't be read from the file.

Christophe
  • 68,716
  • 7
  • 72
  • 138