I am trying to open a file, write in it and close the file. It previously worked with just regular sentences that weren't pointers, but for some reason with the pointers, the file is not even created. Here is my code
#include "std_lib_facilities_5.h"
#include <iosfwd>
class ScoreKeeper {
public:
// Variables
int Score;
string Name;
// Functions
void SetScore(int number)
{
Score = number;
return;
}
void SetName( string name)
{
Name = name;
return;;
}
ScoreKeeper( int nn = 0, string ss =" "): Score(nn), Name(ss){} // Constructor
~ScoreKeeper() {}; // Destructor
friend ostream& operator<< (ostream &out, const ScoreKeeper &player);
};
//----------------------------------------------------------------------------------
ostream& operator<<(ostream& os, const ScoreKeeper& player){
os << player.Name << " " << player.Score;
return os;
}
//----------------------------------------------------------------------------------
int main() {
vector<ScoreKeeper*> Players;
Players.push_back(new ScoreKeeper(300,"Pape"));
Players.push_back(new ScoreKeeper(200,"Yama"));
//--------------------------------------------------------------------------------
for(int i = 0; i < Players.size(); ++i){
cout << "Player: " << Players[i]->Name << " " << Players[i]->Score << endl;
cout << "Score:" << Players[i]->Score << endl;
}
//--------------------------------------------------------------------------------
ofstream ost {"scores.txt"};
if(!ost) error("can't open output file ", "scores");
ost << Players[0]->Name<< " " << Players[0]->Score << endl;
ost.close();
}
Aucun commentaire:
Enregistrer un commentaire