我正在制作一个基于Starcraft的简单C ++游戏。这是练习指针的方法。
该程序运行良好,因此在这种情况下,我现在添加了一些技术性内容,即“隐身能力”幽灵
在幽灵类中,我为while bool cloak == true设置了一个while循环,您将命中设置为空白,因为在隐身时无法击中幽灵(此游戏中没有检测器)当我进行设置时,它给了我错误“一会儿之前会出现预期的不合格ID”。如果我退出循环,它不会给我一个错误。
任何帮助深表感谢
这是我的ghost.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#include "ghost.h"
ghost::ghost(string iname, string iteam, string itype, int Snipe, bool cloak)
: infantry(iname, iteam, itype)
{
set_SniperR(Snipe);
set_Cloak(cloak);
set_health(80);
}
void ghost::set_SniperR(int Snipe)
{
SniperR = Snipe;
}
int ghost::get_SniperR() const
{
return SniperR;
}
void ghost::shoot_SniperR(infantry* attacked_infantry)
{
if(SniperR!=0 && this->get_health()!=0 && attacked_infantry->get_health()!=0)
{
attacked_infantry->SniperR_hit();
}
}
void ghost::attack(infantry* attacked_infantry)
{
shoot_SniperR(attacked_infantry);
if (attacked_infantry->get_health() == 0)
attacked_infantry->die();
}
void ghost::heal(infantry* attacked_infantry) { }
void ghost::die()
{
set_SniperR(0);
}
void ghost::set_Cloak(bool cloak)
{
Cloak = cloak;
}
bool ghost::get_Cloak() const
{
return Cloak;
}
while ( cloak) // <-- error
{
void ghost::AssaultR_hit()
{
// when cloak is on , AssaultR doesnt affect Ghost
}
void ghost::FlameT_hit() { }
void ghost::SniperR_hit() { }
void ghost::RocketL_hit() { }
void ghost::StickyG_hit() { }
}
void ghost::print() const
{
cout << endl;
infantry::print();
cout << "Sniper Rifle Rounds: " << get_SniperR() << endl;
}
void ghost::speak() const
{
infantry::speak();
cout << "Did somebody call for an exterminator? " << endl;
}
void ghost::display() const
{
infantry::display();
cout << right << setw(5) << " "
<< right << setw(5) << " "
<< right << setw(10) << get_SniperR()
<< endl;
}
这样做的正确方法是删除while循环,并检查方法中的斗篷是否正确。这是一个不会给您带来错误的实现(假设斗篷是成员变量,在这种情况下应为该变量):
void ghost::AssaultR_hit()
{
if(!cloak)
{
//assualtR_hit implementation goes here
}
}
void ghost::FlameT_hit()
{
if(!cloak)
{
//FlameT_hit implementation goes here
}
}
void ghost::SniperR_hit()
{
if(!cloak)
{
//SniperR_hit implementation goes here
}
}
void ghost::RocketL_hit()
{
if(!cloak)
{
//RocketL_hit implementation goes here
}
}
void ghost::StickyG_hit()
{
if(!cloak)
{
//StickyG_hit implementation goes here
}
}
注意:还要注意这样的注释:您不能在C ++中的函数外部有while循环,如注释程序所述。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句