while c++之前应为不合格id

expected unqualified id before while c++

本文关键字:不合格 id c++ while      更新时间:2023-10-16

我正在制作一款基于星际争霸的简单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循环。