while c++之前应为不合格id
expected unqualified id before while c++
我正在制作一款基于星际争霸的简单C++游戏。这是一种练习指针的方法。
这个程序运行得很好,所以现在我添加了一些技术性的东西,在这种情况下,伪装能力幽灵
在ghost类中,我为while设置了一个while循环bool斗篷==true,你将命中数设置为空白,因为ghost在隐身时无法命中(这个游戏中没有检测器)。当我设置它时,它会给我错误"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循环并检查方法中的斗篷是否为true。以下是一个不会给您带来错误的实现(假设斗篷是一个成员变量,在这种情况下应该是这样):
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循环。
相关文章:
- 方法内部但循环仍得到预期的不合格id错误C++
- C 预期的不合格ID(类继承)
- 解析问题:预期的不合格的ID和语义问题:C 需要所有声明的类型说明符
- 字符串常量(包括ZenLib/Ztring.h)之前应为不合格id
- Arduino-应为不合格的id
- int之前应为不合格id
- while c++之前应为不合格id
- 预期的不合格id错误,很可能是原因
- 标头C++中应为不合格Id
- 不合格的ID和模板类
- C++中类出现预期的不合格id错误
- QDir目录之前应为不合格的id
- 第25行应为不合格的-id,我该如何更正
- 错误:“int”之前应为不合格的id
- iOS中出现不合格的id错误
- 奇怪的模板错误:应为不合格id
- C++:错误:在“!”之前应为不合格的id代币
- 错误:在if之前需要不合格的id
- 不合格的id错误和井字游戏板不工作
- 在int j之前预期的不合格id.我知道它可能偏离主题,但我找不到错误