Click here to Skip to main content
15,942,847 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm a beginner C++ programmer, and I'm trying to develop by writing a little text game.
Right now I'm trying to write a nice class for creating and managing a player profile, and have recently ran into an issue while finding a way to save and load player variables using fstream.

Here's an example of how I want this class to look:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class cPlayer;

class cPlayer
{
protected:
    string strName;
    int nSomeInteger;
    int nSomeInteger2;
public:
    cPlayer(){}
    cPlayer(string Name)
    {
        strName=Name;
        nSomeInteger=10;
        nSomeInteger2=20;
    }

    string ShowName()      {return strName;}
    int ShowSomeInteger()  {return nSomeInteger;}
    int ShowSomeInteger2() {return nSomeInteger2;}

    void SavePlayer();
    void LoadPlayer();
};

void cPlayer::SavePlayer()
{
    string strFileName=ShowName()+".dat";
    ofstream ofsSavePlayer(strFileName.c_str());
    ofsSavePlayer<<ShowName()<<endl<<ShowSomeInteger()<<endl<<ShowSomeInteger2();
}

void cPlayer::LoadPlayer()
{
    string strFileName=ShowName()+".dat";
    ifstream ifsLoadPlayer(strFileName.c_str());
    ifsLoadPlayer>>ShowName()>>endl>>ShowSomeInteger()>>endl>>ShowSomeInteger2();
}

int main()
{
    string strString;
    cin>>strString;
    cPlayer(strString);

    // - rest of the code that tests whether saving and loading went successfully. 
    // Not important right now.

    return 0;
}




Now, the problem is as following:

|46|error: no match for 'operator>>' in 'std::operator>><char, std::char_traits<char>, std::allocator<char> >((* & ifsLoadPlayer.std::basic_ifstream<char>::<anonymous>), (* &((cPlayer*)this)->cPlayer::strName)) >> std::endl'|


It's basically this line:

ifsLoadPlayer>>strName>>endl>>nSomeInteger>>endl>>nSomeInteger2;


How can I make it work?

Thanks in advance for any kind advice. :)
Posted
Updated 28-Aug-13 0:29am
v3

1 solution

Quote:
ifsLoadPlayer>>ShowName()>>endl>>ShowSomeInteger()>>endl>>ShowSomeInteger2();

You cannot do that. You have to write something like
C++
ifsLoadPlayer>>strName>>nSomeInteger>>nSomeInteger2;


I rewrote your code in roder to make it 'working'. However, for a robust implementation you should study serialization[^].

C++
#include <iostream>
#include <fstream>
#include <string>
 
using namespace std;
 
class cPlayer;
 
class cPlayer
{
protected:
    string strName;
    int nSomeInteger;
    int nSomeInteger2;
public:
    cPlayer(){}
    cPlayer(string Name)
    {
        strName=Name;
        nSomeInteger=10;
        nSomeInteger2=20;
    }
 
    string ShowName()      {return strName;}
    int ShowSomeInteger()  {return nSomeInteger;}
    int ShowSomeInteger2() {return nSomeInteger2;}
 
    void SavePlayer(const string & fname);
    void LoadPlayer(const string & fname);
};
 
void cPlayer::SavePlayer(const string & fname)
{
    string strFileName=fname+".dat";
    ofstream ofsSavePlayer(strFileName.c_str());
    ofsSavePlayer<<strName<< endl <<nSomeInteger << endl<<nSomeInteger2;
}
 
void cPlayer::LoadPlayer(const string & fname)
{
    string strFileName=fname+".dat";
    ifstream ifsLoadPlayer(strFileName.c_str());
    ifsLoadPlayer>>strName>>nSomeInteger>>nSomeInteger2;

}
 
int main()
{
    string strString;
    cin>>strString;
    cPlayer p1(strString);
    p1.SavePlayer(p1.ShowName()); // save player 1

    cPlayer p2;
    p2.LoadPlayer(p1.ShowName()); // load player 2 members  from (player 1) file

    return 0;
}</string></fstream></iostream>
 
Share this answer
 
Comments
StayVigilant 28-Aug-13 7:20am    
Ah - there it is! I reckon I can use >>"\n" instead of >>endl, correct?

I shall read up on serialization, thank you kindly. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900