C++ 未设置变量并返回异常数字

C++ 未设置变量并返回异常数字,c++,function,class,C++,Function,Class,我有一个函数,用来计算对物体造成的伤害 string Weapon::attack(int Damage, int ExtraDamage, string Type) { srand(time(NULL)); int totalDamage = 0; int percentileDie = rand() % 100 + 1; if (percentileDie <= ChanceToHit) { for (int i = 0; i &

我有一个函数,用来计算对物体造成的伤害

string Weapon::attack(int Damage, int ExtraDamage, string Type)
{
    srand(time(NULL));

    int totalDamage = 0;

    int percentileDie = rand() % 100 + 1;

    if (percentileDie <= ChanceToHit) {

        for (int i = 0; i <= Damage; i++)
        {
            int sixSidedDie = rand() % 6 + 1;
            totalDamage = totalDamage + sixSidedDie;
        }
        totalDamage = totalDamage + ExtraDamage;
    }
    else {
        totalDamage = 0;
    }

    Result = totalDamage;

    if (Type == "Crossbow") {
        return "Twang! ";
    }
    else if (Type == "Dagger" || Type == "Sword") {
        return "Swing! ";
    }

}
我得到了正确的号码,但如果我这样做:

Weapon t;
t.attack(2, 4, "Sword");
cout << t.attack(2, 4, "Sword") << t.Result << endl;
Weapon t;
cout << t.attack(2, 4, "Sword") << t.Result << endl;

cout的求值顺序
cout的求值顺序如果你仔细观察这里到底发生了什么,那么你就会意识到操作符“如果你仔细观察这里到底发生了什么,那么你就会意识到操作符“Can
Type
可以是“十字弓”、“匕首”或“剑”以外的东西”?这不是相关代码。显示更多。请编辑您的问题,并发布一个。发布真实代码,这不会编译,
结果
不会在任何地方声明。向我们显示
结果
的声明。可以
类型
可以是除“十字弓”、“匕首”或“剑”以外的其他东西?这不是相关代码。显示更多。请编辑您的问题,并发布一个。发布真实代码,这不会编译,
结果
不会在任何地方声明。向我们显示
结果
的声明。
class Weapon {
public:
    string Name;
    int Damage, ExtraDamage, Result;
    float ChanceToHit;

    string attack(int,int,string);
};
Weapon t;
cout << t.attack(2, 4, "Sword") << t.Result << endl;
Weapon t;
std::string str = t.attack(2, 4, "Sword");
cout << str << t.Result << endl;
cout<<firstOperand<<secondOperand<<thirdOperand;
cout(<<firstOperand(<<secondOperand(<<thirdOperand)));