程序没有完成返回值3221225477没有编译错误

时间:2017-11-14 21:43:24

标签: c++

您好我是c ​​++编程的新手,我已经编写了这段代码

#include <iostream>
#include <string>

using namespace std;

class Weapon
{
    string weaponName;
public:
    Weapon(string n)
    {
        weaponName=n;
        cout<<"Weapon created"<<endl;
    };

    ~Weapon()
    {
        cout<<"Weapon destroyed"<<endl;
    };

    //getter

    string getWeaponName()
    {
        return weaponName;
    };

    //setter

    string setWeaponName(string name2)
    {
        weaponName=name2;
    };


};

class Samurai 
{   
    string samuraiWeapon;
    string name;
    int age;
    int numberOfWins;
    int numberOfInjuries;
    int numberOfDuels;
 public:    
    Samurai(string t,int w,int i,int d,int a)
    {
        name=t;
        numberOfWins=w;
        numberOfInjuries=i;
        numberOfDuels=d;
        age=a;
        samuraiWeapon="no weapon";
        cout <<"Samurai ready for duel."<<endl;


    };

    ~Samurai()
    {
        cout<<"Samurai " << name <<" deleted "<<endl;
    };

    void  printSamuraiDescription()
    {
        cout<<"Samurai name: "<< name <<" Duels: "<<numberOfDuels<<" Wins: "<<numberOfWins<<"  Injuries: "<<numberOfInjuries<<endl;
    };

    void pickWeapon(Weapon &a)
    {
        if (age<18)
        {
            setSamuraiWeapon("Wooden Sword");
            cout<<samuraiWeapon<<endl;
        }
        else
        {
        setSamuraiWeapon(a.getWeaponName());
            cout<<samuraiWeapon<<endl;  
        }
    }

    //getters

    string getName()
    {
        return name;
    };

    string getSamuraiWeapon()
    {
        return samuraiWeapon;
    };

    int getnumberOfWins()
    {
        return numberOfWins;
    };

    int getnumberOfInjuries()
    {
        return numberOfInjuries;
    };

    int getnumberOfDuels()
    {
        return numberOfDuels;
    };

    int getAge()
    {
        return age;
    };

    //setters

    string setName(string a)
    {
         name=a;
    };

    string setSamuraiWeapon(string e)
    {
        samuraiWeapon=e;
    };

    int setnumberOfWins(int b)
    {
         numberOfWins=b;
    };

    int setnumberOfInjuries(int c)
    {
         numberOfInjuries=c;
    };

    int setnumberOfDuels(int d)
    {
         numberOfDuels=d;
    };

    int setAge(int f)
    {
        age=f;
    };

    //friend fuction
    friend void duel(Samurai &a, Samurai &b, bool winner);
    friend string duelForYoungSamurais(Samurai &a, Samurai &b);


};

string duelForYoungSamurais(Samurai &a, Samurai &b)
{
    if (a.age>18 || b.age>18)
    {
        cout<<"Invalid duel"<<endl;
    }

    if (a.samuraiWeapon!="Wooden Sword" || b.samuraiWeapon!="Wooden Sword")
    {
        cout<<"Duel postponed"<<endl;   
    }
    else
    {
        cout<<a.getName()<<" duels "<<b.getName()<<endl;
    }


}

void duel(Samurai &a, Samurai &b, bool winner)
{
    a.setnumberOfDuels(a.numberOfWins+1);
    b.setnumberOfDuels(b.numberOfWins+1);

    if (winner==true)
    {
        cout<<"Samurai a is winner "<<endl;
        a.setnumberOfWins(a.numberOfWins+1);
        b.setnumberOfInjuries(b.numberOfInjuries+1);
        if(a.numberOfWins>=10) cout<<"Samurai a is duelist"<<endl;
    }
    else
    {
        cout<<"Samurai b is winner "<<endl;
        b.setnumberOfWins(b.numberOfWins+1);
        a.setnumberOfInjuries(a.numberOfInjuries+1);
        if(b.numberOfWins>=10) cout<<"Samurai b is duelist"<<endl;  
    }


}

int main()
{
    Samurai a("Giorgos",9,0,0,18);
    Samurai b("Giannis",1,2,3,13);
    Weapon a1("Rock");
    a.pickWeapon(a1);
    b.pickWeapon(a1);
    duelForYoungSamurais(a,b);
    duel(a,b,true);
    a.printSamuraiDescription();
    b.printSamuraiDescription();


}

程序编译没有错误,但是当它启动并运行时停止并在几秒钟结束时不执行我想要的操作并返回3221225477如果我在评论中放入与武器类和pickWeapon和duelForYoungSamurais有关的部分功能我的代码运行得很好

1 个答案:

答案 0 :(得分:2)

在这里,我更正了除void以外的返回类型的所有setter,现在它可以正常工作:

#include <iostream>
#include <string>

using namespace std;

class Weapon
{
    string weaponName;
public:
    Weapon(string n)
    {
        weaponName=n;
        cout<<"Weapon created"<<endl;
    }

    ~Weapon()
    {
        cout<<"Weapon destroyed"<<endl;
    }

    //getter

    string getWeaponName()
    {
        return weaponName;
    }

    //setter

    void setWeaponName(string name2)
    {
        weaponName=name2;
    }


};

class Samurai 
{   
    string samuraiWeapon;
    string name;
    int age;
    int numberOfWins;
    int numberOfInjuries;
    int numberOfDuels;
    public:

    Samurai(string t,int w,int i,int d,int a)
    {
        name=t;
        numberOfWins=w;
        numberOfInjuries=i;
        numberOfDuels=d;
        age=a;
        samuraiWeapon="no weapon";
        cout <<"Samurai ready for duel."<<endl;


    };

    ~Samurai()
    {
        cout<<"Samurai " << name <<" deleted "<<endl;
    }

    void  printSamuraiDescription()
    {
        cout<<"Samurai name: "<< name <<" Duels: "<<numberOfDuels<<" Wins: "<<numberOfWins<<"  Injuries: "<<numberOfInjuries<<endl;
    }

    void pickWeapon(Weapon &a)
    {
        if (age<18)
        {
            setSamuraiWeapon("Wooden Sword");
            cout<<samuraiWeapon<<endl;
        }
        else
        {
        setSamuraiWeapon(a.getWeaponName());
            cout<<samuraiWeapon<<endl;  
        }
    }

    //getters

    string getName()
    {
        return name;
    }

    string getSamuraiWeapon()
    {
        return samuraiWeapon;
    }

    int getnumberOfWins()
    {
        return numberOfWins;
    }

    int getnumberOfInjuries()
    {
        return numberOfInjuries;
    }

    int getnumberOfDuels()
    {
        return numberOfDuels;
    }

    int getAge()
    {
        return age;
    }

    //setters

    void setName(string a)
    {
         name=a;
    }

    void setSamuraiWeapon(string e)
    {
        samuraiWeapon=e;
    }

    void setnumberOfWins(int b)
    {
         numberOfWins=b;
    }

    void setnumberOfInjuries(int c)
    {
         numberOfInjuries=c;
    }

    void setnumberOfDuels(int d)
    {
         numberOfDuels=d;
    }

    void setAge(int f)
    {
        age=f;
    }

    //friend fuction
    friend void duel(Samurai &a, Samurai &b, bool winner);
    friend void duelForYoungSamurais(Samurai &a, Samurai &b);


};

void duelForYoungSamurais(Samurai &a, Samurai &b)
{
    if (a.age>18 || b.age>18)
    {
        cout<<"Invalid duel"<<endl;
    }

    if (a.samuraiWeapon!="Wooden Sword" || b.samuraiWeapon!="Wooden Sword")
    {
        cout<<"Duel postponed"<<endl;   
    }
    else
    {
        cout<<a.getName()<<" duels "<<b.getName()<<endl;
    }


}

void duel(Samurai &a, Samurai &b, bool winner)
{
    a.setnumberOfDuels(a.numberOfWins+1);
    b.setnumberOfDuels(b.numberOfWins+1);

    if (winner==true)
    {
        cout<<"Samurai a is winner "<<endl;
        a.setnumberOfWins(a.numberOfWins+1);
        b.setnumberOfInjuries(b.numberOfInjuries+1);
        if(a.numberOfWins>=10) cout<<"Samurai a is duelist"<<endl;
    }
    else
    {
        cout<<"Samurai b is winner "<<endl;
        b.setnumberOfWins(b.numberOfWins+1);
        a.setnumberOfInjuries(a.numberOfInjuries+1);
        if(b.numberOfWins>=10) cout<<"Samurai b is duelist"<<endl;  
    }


}

int main()
{
    Samurai a("Giorgos",9,0,0,18);
    Samurai b("Giannis",1,2,3,13);
    Weapon a1("Rock");
    a.pickWeapon(a1);
    b.pickWeapon(a1);
    duelForYoungSamurais(a,b);
    duel(a,b,true);
    a.printSamuraiDescription();
    b.printSamuraiDescription();


}