将对象指定给指针c++; 我和我的朋友正在C++中制作一个基于文本的游戏,为了好玩,并且要多学一点。我一直在尝试使用指向类的指针,但我运气不好,出现了一些对我来说毫无意义的错误,我希望有人能帮助我
代码:将对象指定给指针c++; 我和我的朋友正在C++中制作一个基于文本的游戏,为了好玩,并且要多学一点。我一直在尝试使用指向类的指针,但我运气不好,出现了一些对我来说毫无意义的错误,我希望有人能帮助我,c++,pointers,C++,Pointers,代码: //Map.h #包括“Player.h” 类图 { //虚拟函数 }; 类StartMap:公共地图 { //代码 }开始; 类JungleMap:公共地图 { //代码 }丛林; 类别河流地图:公共地图 { //代码 }河流; //Player.h #ifndef图 #定义映射 #恩迪夫 职业选手 { 私人: 地图*播放地图; //其他变量 公众: void Initialize() { //初始化变量 PlayerMap=&Start;//这是发生错误的地方,表示存在错误 //*玩
//Map.h
#包括“Player.h”
类图
{
//虚拟函数
};
类StartMap:公共地图
{
//代码
}开始;
类JungleMap:公共地图
{
//代码
}丛林;
类别河流地图:公共地图
{
//代码
}河流;
//Player.h
#ifndef图
#定义映射
#恩迪夫
职业选手
{
私人:
地图*播放地图;
//其他变量
公众:
void Initialize()
{
//初始化变量
PlayerMap=&Start;//这是发生错误的地方,表示存在错误
//*玩家::玩家地图。试过推杆
//此->PlayerMap=&Start,没有帮助
//当我做指针时没有错误
}
//一系列其他函数
}玩家;
好的,这是我决定添加.cpp文件后的代码:
//Command.h
class Command
{
private:
string GameCommand;
void Trim();
public:
Command (string command) {GameCommand = command;}
Command () {}
void operator = (string command) {GameCommand = command;}
void ReadCommand();
string Print();
}
//Command.cpp
#include <iostream>
#include <string>
#include "Command.h"
#include "Parameter.h"
using namespace std;
void Command::Trim()
{
int LeadingPos = 0, MidCount = 0, TrailingPos = GameCommand.length()-1, Size = 0;
string TempCommand = "";
while (GameCommand[LeadingPos] == ' '){LeadingPos += 1;}
while (GameCommand[TrailingPos] == ' '){TrailingPos -= 1;}
Size = ((TrailingPos+1)-LeadingPos);
for (int loops = 0; loops < Size; loops++)
{
if (MidCount > 0 && GameCommand[LeadingPos] == ' ')
{
LeadingPos += 1;
}
else
{
if (GameCommand[LeadingPos] == ' ')
{
MidCount += 1;
}
TempCommand += GameCommand[LeadingPos];
LeadingPos += 1;
}
}
GameCommand = TempCommand;
}
void Command::ReadCommand()
{
Trim();
string Parameter;
if (GameCommand.substr(0,3) == "go ")
{
Parameter = GameCommand.substr(3,string::npos);
CommandParameter.Go(Parameter);
}
else if (GameCommand.substr(0,4) == "dig ")
{
Parameter = GameCommand.substr(4,string::npos);
CommandParameter.Dig(Parameter);
}
else if (GameCommand.substr(0,4) == "eat ")
{
Parameter = GameCommand.substr(4,string::npos);
CommandParameter.Eat(Parameter);
}
else if (GameCommand.substr(0,4) == "exit" || GameCommand.substr(0,4) == "quit")
{
exit(0);
}
else if (GameCommand.substr(0,4) == "use ")
{
Parameter = GameCommand.substr(4,string::npos);
CommandParameter.Use(Parameter);
}
else if (GameCommand.substr(0,5) == "drop ")
{
Parameter = GameCommand.substr(5,string::npos);
CommandParameter.Drop(Parameter);
}
else if (GameCommand.substr(0,5) == "grab " || GameCommand.substr(0,5) == "take ")
{
Parameter = GameCommand.substr(5,string::npos);
CommandParameter.Pickup(Parameter);
}
else if (GameCommand.substr(0,5) == "help ")
{
Parameter = GameCommand.substr(5,string::npos);
CommandParameter.Help(Parameter);
}
else if (GameCommand.substr(0,5) == "look ")
{
Parameter = GameCommand.substr(5,string::npos);
CommandParameter.Look(Parameter);
}
else if (GameCommand.substr(0,5) == "sleep")
{
CommandParameter.Sleep();
}
else if (GameCommand.substr(0,6) == "check ")
{
Parameter = GameCommand.substr(6,string::npos);
CommandParameter.Check(Parameter);
}
else if (GameCommand.substr(0,6) == "climb ")
{
Parameter = GameCommand.substr(6,string::npos);
CommandParameter.Climb(Parameter);
}
else if (GameCommand.substr(0,6) == "throw ")
{
Parameter = GameCommand.substr(6,string::npos);
CommandParameter.Throw(Parameter);
}
else if (GameCommand.substr(0,7) == "attack ")
{
Parameter = GameCommand.substr(7,string::npos);
CommandParameter.Attack(Parameter);
}
else if (GameCommand.substr(0,7) == "search ")
{
Parameter = GameCommand.substr(7,string::npos);
CommandParameter.Search(Parameter);
}
else
{
cout << "Not a valid command.\n";
}
}
string Print()
{
return GameCommand;
}
//Command.h
类命令
{
私人:
字符串命令;
空隙修剪();
公众:
命令(字符串命令){GameCommand=Command;}
命令(){}
void运算符=(字符串命令){GameCommand=command;}
void ReadCommand();
字符串打印();
}
//Command.cpp
#包括
#包括
#包括“Command.h”
#包括“Parameter.h”
使用名称空间std;
void命令::Trim()
{
int LeadingPos=0,MidCount=0,TrailingPos=GameCommand.length()-1,Size=0;
字符串TempCommand=“”;
而(游戏命令[LeadingPos]=''{LeadingPos+=1;}
while(GameCommand[TrailingPos]=''{TrailingPos-=1;}
尺寸=((牵引位置+1)-牵引位置);
for(int循环=0;循环<大小;循环++)
{
如果(中期数>0&&GameCommand[LeadingPos]='')
{
LeadingPos+=1;
}
其他的
{
如果(游戏命令[LeadingPos]='')
{
中期计数+=1;
}
TempCommand+=GameCommand[LeadingPos];
LeadingPos+=1;
}
}
GameCommand=TempCommand;
}
void命令::ReadCommand()
{
修剪();
字符串参数;
如果(GameCommand.substr(0,3)=“go”)
{
参数=GameCommand.substr(3,字符串::npos);
CommandParameter.Go(参数);
}
else if(GameCommand.substr(0,4)=“dig”)
{
参数=GameCommand.substr(4,字符串::npos);
CommandParameter.Dig(参数);
}
else if(GameCommand.substr(0,4)=“eat”)
{
参数=GameCommand.substr(4,字符串::npos);
CommandParameter.Eat(参数);
}
else if(GameCommand.substr(0,4)=“退出”| | GameCommand.substr(0,4)=“退出”)
{
出口(0);
}
else if(GameCommand.substr(0,4)=“使用”)
{
参数=GameCommand.substr(4,字符串::npos);
CommandParameter.Use(参数);
}
else if(GameCommand.substr(0,5)=“drop”)
{
参数=GameCommand.substr(5,字符串::npos);
CommandParameter.Drop(参数);
}
else if(GameCommand.substr(0,5)=“抓取”| | GameCommand.substr(0,5)=“抓取”)
{
参数=GameCommand.substr(5,字符串::npos);
命令参数。拾取(参数);
}
else if(GameCommand.substr(0,5)=“帮助”)
{
参数=GameCommand.substr(5,字符串::npos);
CommandParameter.Help(参数);
}
else if(GameCommand.substr(0,5)=“look”)
{
参数=GameCommand.substr(5,字符串::npos);
CommandParameter.Look(参数);
}
else if(GameCommand.substr(0,5)=“sleep”)
{
CommandParameter.Sleep();
}
else if(GameCommand.substr(0,6)=“检查”)
{
参数=GameCommand.substr(6,字符串::npos);
CommandParameter.Check(参数);
}
否则如果(GameCommand.substr(0,6)=“爬升”)
{
参数=GameCommand.substr(6,字符串::npos);
命令参数。爬升(参数);
}
else if(GameCommand.substr(0,6)=“throw”)
{
参数=GameCommand.substr(6,字符串::npos);
CommandParameter.Throw(参数);
}
else if(GameCommand.substr(0,7)=“攻击”)
{
参数=GameCommand.substr(7,字符串::npos);
CommandParameter.Attack(参数);
}
else if(GameCommand.substr(0,7)=“搜索”)
{
参数=GameCommand.substr(7,字符串::npos);
CommandParameter.Search(参数);
}
其他的
{
库特
语法不正确。您需要
class StartMap : public Map
{
// Details of class
} Start;
您需要对JungleMap
和RiverMap
进行类似的更改,我注意到的第一件事是每次继承声明后的分号。。
classxxxxx:publicmap;
您的代码布局有很多问题
这没有任何作用:
//Player.h
#ifndef MAP_H
#define MAP_H
#endif
我想你是想做一个包含守卫。正确的布局是:
#ifndef PLAYER_H
#define PLAYER_H
// all your code for the header file goes here
class Player
{
// ....
};
#endif // no more code after this line
下一个问题是Player.h
应该包括Map.h
,而不是相反。假设你是编译器。你正在处理Player.h
。你一直到Map*PlayerMap;
。但你不知道是什么<
//Player.h
#ifndef MAP_H
#define MAP_H
#endif
#ifndef PLAYER_H
#define PLAYER_H
// all your code for the header file goes here
class Player
{
// ....
};
#endif // no more code after this line
class StartMap : public Map
{
//Code
};
// the global variables
StartMap start_map;
JungleMap jungle_map;
void Map::Initialize()
{
player_map = &start_map;
}