C++ 二进制文件-obj(游戏)保存并加载obj
这个复制这个地址我不想复制地址,然后我整个过程都释放第一个对象 这个练习课 我必须实现保存和加载游戏寻宝 我的文件: 主要内容: 这是要复制的函数:C++ 二进制文件-obj(游戏)保存并加载obj,c++,C++,这个复制这个地址我不想复制地址,然后我整个过程都释放第一个对象 这个练习课 我必须实现保存和加载游戏寻宝 我的文件: 主要内容: 这是要复制的函数: void TreasureSeeker::save(ofstream & out)const { out.write((const char*)this, sizeof(*this)); } TreasureSeeker::TreasureSeeker(ifstream& in) { in.read((char*)
void TreasureSeeker::save(ofstream & out)const
{
out.write((const char*)this, sizeof(*this));
}
TreasureSeeker::TreasureSeeker(ifstream& in)
{
in.read((char*)this, sizeof(*this));
}
玩家:
#include "GamePiece.h"
#ifndef _Player_H
#define _Player_H
//class player
class Player : public GamePiece
{
public:
Player();
~Player();
void print();
int Neutralization();
int Step();
int getX(){ return this->placeX; }
int getY(){ return this->placeY; }
void setx(int x){this->placeX = x;}
void sety(int y){this->placeY = y;}
void fight();
void findknife();
void setn(string d){ this->nameplayer = d; }
private:
string nameplayer;
int knife;
int placeX;
int placeY;
int killenemy;
int killtrap;
};
#endif
陷阱:
#include"GamePiece.h"
#ifndef _Trap_H
#define _Trap_H
class Trap :public GamePiece
{
public:
Trap();
~Trap();
void puttrap(int x,int y);
int getnumoftrap();
private:
int numoftrap;
};
#endif
宝藏:
#include"GamePiece.h"
#ifndef _Treasure_H
#define _Treasure_H
class Treasure :public GamePiece
{
public:
Treasure(int x, int y);
~Treasure();
int getX();
int getY();
int Gettrusefake();
private:
int TreasureX;
int TreasureY;
int Treasurefake;
};
#endif
borad来自这一类:
#define _CRT_SECURE_NO_WARNINGS
#ifndef _GamePiece_H
#define _GamePiece_H
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <ctype.h>
#include <time.h>
#include <typeinfo>
#include <stdlib.h>
using namespace std;
//class gamepiece +func get set print
class GamePiece
{
public:
GamePiece(){ nametool = "Empty Cell"; charname = 'O'; }
~GamePiece(){};
void printname(){ cout << "name tool is :" << nametool << endl; }
void printchar(){ cout << charname; }
virtual void step(){};
char getname(){ return charname; }
string getoolname(){ return nametool; }
void setcharname(char p){ charname = p; }
private:
string nametool;
char charname;
};
#endif
这就是游戏的功能:
cout << "Hello to Find the Treasure game" << endl;
int save = 1;
do
{
cout << endl;
p1->print();
cout << endl;
print();
step();
cout << endl;
print();
cout << "to save end exit prees 0 to continue prees 1 :" << endl << " your choise : ";
cin >> save;
} while (save);
}
只有当类的所有数据成员都拥有且不拥有资源时,才可以这样做,即使这样,也会出现资源的问题
免费的建议,不要明确地这样做,它通常是不可移植的,即使对于不拥有资源的POD类型也是如此
注意:指针拥有或引用资源内存
不同的编译器可能会在内存中以不同的方式进行编译和布局
TreasureSeeker::TreasureSeeker(ifstream& in)
{
in.read((char*)this, sizeof(*this));
}
除了我之前说过的,这也是一个非常糟糕的想法。你处于这个环境中,并且在外部修改这个整体,而它是活的
要解决您的问题,请阅读关于此问题是否加密?有人知道私钥是什么吗?是什么让你认为你可以简单地将你的类序列化成一个文件,然后再检索回来?指针无法重新创建。因此,除了保存和加载游戏外,你能指导我做什么吗?因为这是一门课程的练习,你需要知道的一切都应该包含在课程材料中。文章网络选择-10分:实现游戏的保存和加载,使用二进制文件。
cout << "Hello to Find the Treasure game" << endl;
int save = 1;
do
{
cout << endl;
p1->print();
cout << endl;
print();
step();
cout << endl;
print();
cout << "to save end exit prees 0 to continue prees 1 :" << endl << " your choise : ";
cin >> save;
} while (save);
}
void TreasureSeeker::save(ofstream & out)const
{
out.write((const char*)this, sizeof(*this));
}
TreasureSeeker::TreasureSeeker(ifstream& in)
{
in.read((char*)this, sizeof(*this));
}