我正试图在我的程序中建立一个保存/加载函数。我把存钱的部分放下了,效果很好。我正在尝试反向保存和加载对象。我把文件传递给了object方法,但是我做错了一些事情。我不知道如何从文件中的数据中分配私有属性。
case '2':// load character
game = load();
if(game == 4 || game == 5 || game == 6){
newgame(game-3);
}
else{
hero herosave;
loadgame(game, herosave);
}
break;void loadgame(int save, hero newhero){
if(save == 1){
ifstream file1("hero1.txt");
newhero.loadStats(file1);
file1.close();
}
if(save == 2){
ifstream file2("hero2.txt");
newhero.loadStats(file2);
file2.close();
}
if(save == 3){
ifstream file3("hero2.txt");
newhero.loadStats(file3);
file3.close();
}
}class hero{
private:
string name;
string race;
string heroclass;
int strength, wisdom, charisma;
int dexterity, intelligence, constitution;
int hitpoints, AC;
public:
hero();
void setName(string);
void setRace(int);
void setClass(int);
void setStats();
string getRace();
string getClass();
string getName();
int getStr();
int getWis();
int getCha();
int getDex();
int getInt();
int getCon();
int setAC();
void loadStats(ifstream &);
};void hero::loadStats(ifstream &file){
file >> name;
//THIS IS WHERE IM HAVING ISSUES
}Bilbo Baggins
Halfling
Rogue
16
13
6
9
7
16发布于 2022-08-10 00:03:04
我未能在包含类英雄的头文件中#include <fstream>。#include <fstream>位于包含函数的头文件中,具有#include <fstream>,但它没有包含在类头文件中。
https://stackoverflow.com/questions/73299095
复制相似问题