Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从派生类中的方法从基类调用方法?_C++_Class_Inheritance - Fatal编程技术网

C++ 如何从派生类中的方法从基类调用方法?

C++ 如何从派生类中的方法从基类调用方法?,c++,class,inheritance,C++,Class,Inheritance,我有一个名为“巫师”的基类,我试图在witch或wizard类中调用函数takeStrength。我该怎么做?我以为那只是巫师:takeStrength(s.Sorcher)但我无法让它工作。编译错误是当我尝试使用sorcer::takeStrength(s.sorcerer)时,使用'sorcer::sorcer'无效从witch类的fight函数中调用takeStrength。我想它和我调用函数的语法一样简单,但在尝试了不同的方法后,我不再确定了。提前感谢您的帮助 巫师h: class so

我有一个名为“巫师”的基类,我试图在witch或wizard类中调用函数takeStrength。我该怎么做?我以为那只是巫师:takeStrength(s.Sorcher)但我无法让它工作。编译错误是当我尝试使用
sorcer::takeStrength(s.sorcerer)时,使用'sorcer::sorcer'无效
从witch类的fight函数中调用takeStrength。我想它和我调用函数的语法一样简单,但在尝试了不同的方法后,我不再确定了。提前感谢您的帮助

巫师h:

class sorcerer
// models a sorcerer
{
public:
    sorcerer();
    // default constructor, sets name to empty string and strength to 0

    sorcerer(string initName, double initStrength);
    // constructor; sets name to the value of initName; if initStrength > 0
    // strength is set to the value of initStrength, otherwise strength is 
    // set to .1

    string getName();
    // returns the value of name
    // postcondition: the value of name is returned

    double getStrength();
    // returns the value of strength
    // postcondition: the value of strength is returned

    void   takeStrength(sorcerer & s);    
    // takes half the strength of the sorcerer represented by parameter s and 
    // adds it to strength
    // precondition:  s is passed a valid sorcerer object or an object from a  
    //                class that inherits class sorcerer
    // postcondition: strength is increased by half of the strength of the 
    //                object passed to s; the object passed to s has its 
    //                strength reduced by half

    virtual void fight(sorcerer & s) = 0;
    // pure virtual function, makes class sorcerer abstract so that class 
    // sorcerer must be inherited and function fight must be implemented
    //
    // postcondition: a fight has occurred between the calling sorcerer and the
    //                passed sorcerer

private:
    double strength;  // sorcerer strength; 0 or greater
    string name;      // sorcerer name
};
W.h:

class witch : public sorcerer
// inherits class sorcerer in order to model a witch
{
public:
    witch();
    // default constructor
//    // calls default constructor of class sorcerer

    witch(string name, double initStrength);
    // constructor
    // calls parametrized constructor of classs sorcerer to initialize strength
    // and name

    void fight(sorcerer & s);
    // fights a sorcerer (an instance object of any class that inherits class 
    // sourcerer) on the witch's home turf
    //
    // postcondition:  A fight has occured between the witch and the passed
    //                 sourcerer.  A tie halts the function.  If there is a 
    //                 winner, the winner's strength is increased by 1/2 of the 
    //                 strength of the loser and the loser's strenght is 
    //                 decreased by half.
};

注意:wizard.h与witch相同,但有一些细微的区别,您没有显示您试图从何处调用
takeStrength()
,但我假设它在您的
witch::fight()
wizard::fight()
实现中,在这种情况下,您可以根据需要这样调用它:

void witch::fight(sorcerer & s)
{
    ...
    // taking strength away from the other sorcerer
    // and giving it to this sorcerer
    takeStrength(s);
    ...
    // taking strength away from this sorcerer
    // and giving it to the other sorcerer
    s.takeStrength(*this);
    ...
}


“我不能让它工作”-那么它不工作怎么办?您是否有编译时错误,如果有,是哪些错误。“无法使其工作”不是问题描述。这具体意味着什么?你很清楚,否则你就不会觉得有必要在这里发帖了,所以你没有理由不解释为什么它在你的帖子里不起作用。如果您需要帮助,您应该提供您已经向我们提供的相关信息,以便我们使用。请提供再现问题的最小示例。尝试将
s.sorcher
替换为
s
。看起来,您想进行类型转换,但这并不是必需的,而且以您的方式是不正确的。(
(巫师)s
是类型转换,而不是您试图做的。)@tmricks94:
此*
需要改为
*此
。看看我的答案。这就是我一直在寻找的。谢谢你,伙计。顺便说一句,我说了我从哪个函数调用“从战斗函数内部调用takeStrength”。
void wizard::fight(sorcerer & s)
{
    ...
    // taking strength away from the other sorcerer
    // and giving it to this sorcerer
    takeStrength(s);
    ...
    // taking strength away from this sorcerer
    // and giving it to the other sorcerer
    s.takeStrength(*this);
    ...
}