未定义的标识符

时间:2012-03-04 01:31:17

标签: c++

我正在学习C ++并制作了一个脚本,一个非常基本的玩家攻击怪物类型的东西。但我得到未定义的标识符问题= /

我不太明白为什么,我来自PHP背景,所以它可能不完全适应C ++严格的方式,但我希望有人可以解释。

错误:

main.cpp(30): error C2065: 'miss' : undeclared identifier
main.cpp(31): error C2065: 'hit' : undeclared identifier
main.cpp(43): error C2065: 'pmiss' : undeclared identifier
main.cpp(46): error C2065: 'phit' : undeclared identifier
main.cpp(47): error C2065: 'phit' : undeclared identifier
main.cpp(49): error C2065: 'mmiss' : undeclared identifier
main.cpp(52): error C2065: 'mhit' : undeclared identifier
main.cpp(53): error C2065: 'mhit' : undeclared identifier

我的剧本:

#include <iostream>


int player_health = 10;
int monster_health = 10;

void main(){

    do{
        std::cout << "#####################################" << std::endl;
        std::cout<< "Pick you're weapon and hit enter!" << std::endl;
        std::cout << "Press k for knife or b for Bat!" << std::endl;
        std::cout <<""<<std::endl;
        std::cout << "Player Health: "<< player_health << std::endl;
        std::cout << "Monster Health: "<< monster_health << std::endl;
        std::cout << "###################################"<< std::endl;
        std::cout<<""<<std::endl;

        char user_weapon;
        std::cin >> user_weapon;

        if(user_weapon == 'k' || user_weapon == 'b'){
            if(user_weapon == 'k'){
                int miss = 5;
                int hit = 5;
            } else if(user_weapon == 'b'){
                int miss = 2;
                int hit =3;
            }
                if((rand() % miss) < 3){
                    int phit = (rand()% hit);
                } else {
                    bool pmiss = true;
                }

            //monster
            if((rand() % 5) < 3){
            int mhit = (rand()%3);
            } else {
                bool mmiss = true;
            }

            if(pmiss){
                std::cout << "Player missed monster!"<<std::endl;
            }else{
                monster_health = monster_health - phit;
                std::cout << "Player hit monster for " << phit << "!" << std::endl;
            }
            if(mmiss){
            std::cout << "Monster missed player!"<<std::endl;
            } else{
                player_health = player_health - mhit;
            std::cout << "Monster hit for " << mhit << "!" <<std::endl;
            }
        } else {
            std::cout << "Invalid input , try either k or b" << std::endl;
        }


    }while(player_health >0 || monster_health > 0);
        std::cout << "###################################"<< std::endl;
        std::cout<<""<<std::endl;
    if(player_health < 0 && monster_health < 0){
        std::cout << "It's a draw!" << std::endl;
    } else if (player_health > monster_health){
        std::cout<<"Player Wins!" << std::endl;
    } else {
        std::cout<<"Monster Wins!" << std::endl;
    }
        std::cout << "###################################"<< std::endl;
        std::cout<<""<<std::endl;
}

如果你运行脚本,你应该得到与我相同的错误。

1 个答案:

答案 0 :(得分:5)

if(user_weapon == 'k'){
                int miss = 5;
                int hit = 5;
            } else if(user_weapon == 'b'){
                int miss = 2;
                int hit =3;
            }
                if((rand() % miss) < 3){

您在miss的范围内定义if,然后在范围外使用它,因此在if((rand() % miss) < 3){中,您得到miss未定义 - 因为它仅在您声明它的范围内定义。

请注意,在c ++中,你不能这样做,c ++中有静态作用域。

您应该在miss范围之前定义if,并且只在那里指定一个值。

同样也适用于其他变量,例如hitpmiss