哈罗,第一次问一个问题!我有3个类:CharacterBuilder(它是抽象类),BullyCharacterBuilder(它是从CharacterBuilder继承并重新定义其功能之一)和Character(字符),这是一个通用类,里面充满了带有我需要使用的访问器/变量的int。

我在CharacterBuilder.h中定义一个指向Character的指针,如下所示:

    #ifndef CHARACTERBUILDER_H
    #define CHARACTERBUILDER_H
    #include "Character.h"

    class CharacterBuilder {
    public:
        Character* getCharacter();
        void createNewCharacter();
        virtual void buildCharacter(int level) = 0;

    protected:
        Character* leeroy;

    };
    #endif


CharacterBuilder.cpp

 #include "CharacterBuilder.h"
    #include "Character.h"

    Character* leeroy;

    Character* CharacterBuilder::getCharacter() {
        return leeroy;
    }
    void CharacterBuilder::createNewCharacter(){
        leeroy = new Character;
    }


然后,我在BullyCharacterBuilder类中从CharacterBuilder公开继承,如下所示:

#ifndef BULLYCHARACTERBUILDER_H
#define BULLYCHARACTERBUILDER_H

#include "Character.h"
#include "CharacterBuilder.h"

class CharacterBuilder;

class BullyCharacterBuilder : public CharacterBuilder{

    CharacterBuilder* myParent;

 void buildCharacter(int level);
};
#endif


最后,在我的BullyCharacterBuilder的cpp文件中,我尝试从Character指针(假定我已经从CharacterBuilder继承)中调用访问器/变异器,如下所示:

#include "Character.h"
#include "BullyCharacterBuilder.h"
#include "CharacterBuilder.h"

void buildCharacter(int level){
leeroy->setAbilityScores();
}


尝试执行此操作时,出现错误:标识符“ leeroy”未定义
这可能是一个愚蠢的问题,但我尝试使用我能想到的每种关键字组合在stackoverflow和google上搜索此特定问题。将非常有帮助!

最佳答案

您忘记了使用类名称buildCharacter限定函数BullyCharacterBuilder的资格:

void BullyCharacterBuilder::buildCharacter(int level){
leeroy->setAbilityScores();
}

07-27 13:36