-6
class fairy 
    {
    protected:
        bool x;
        int points;
    public:
        fairy();
        void intro(string, int);
        void call_decision(int, int);
    };

    fairy::fairy()
    {
        x = true;
        points = 0;
    }

    void fairy::intro(string NAME, int case_type)
    {
        int decision = 0;
        cout << "Hello, my name is" << NAME << "." << endl << "What would you like to do" << endl;
        while(x)
        {
        cout << endl;
        cout << "(1) Ask for a wish." << endl;
        cout << "(2) Offer to help the fairy." << endl;
        cout << "(3) Say goodbye to the fairy." << endl;
        cin >> decision;
        call_decision(decision, case_type);
        }
    }

    class good : fairy
    {
    protected:
        int wish_num;
        bool HELP;
    public:a
        good();
        static string name;
        void wish();
        void help();
    };

    good::good()
    {
        name = "Sandy";
        wish_num = 0;   
        HELP = true;
    }

    void good::wish()
    {
        if (wish_num != 5)
        {
        cout << "Your wish has been granted" << endl;
        points += 10;
        wish_num++;
        }
        else
        {
            cout << "Sorry, I can not grant you another wish" << endl;
        }
    }

    void good::help()
    {
        if (HELP)
        {
            cout << "THANK YOU" << endl;
            points += 50;
            HELP = false;
        }
        else
        {
            cout << "Thank you but I don't need anymore help." << endl;
        }
    }

    class evil : fairy
    {
    protected:

    public:
        evil();
        static string name;
        void wish();
        void help();
    };

    evil::evil()
    {
        name = "Seth";
    }

    void evil::wish()
    {
        cout << "NO!!!! MUHHHHHAAAA!!!" << endl;
        points -= 10;
    }

    void evil::help()
    {
            cout << "Thanks... NOT, I don't even want your help!!" << endl;
            points -= 50;
    }

    class equivocal : fairy
    {
    protected:
        int wish_num;
    public:
        equivocal();
        static string name;
        void wish();
        void help();
    };

    equivocal::equivocal()
    {
        name = "Joe";
        wish_num = 0;
    }

    void equivocal::wish()
    {
        int random = (rand() % 2);
        if (random && wish_num != 3)
        {
        cout << "Your wish has been granted" << endl;
        points += 10;
        wish_num++;
        }
        else
        {
            cout << "Sorry, I can not grant you another wish" << endl;
            points -= 10;
        }
    }

    void equivocal::help()
    {
        cout << "I'll think about it" << endl;
    }

    void fairy::call_decision(int decision, int case_type)
    {
    good one;
    evil two;
    equivocal three;

        {
        switch(decision)
        {
        case'1':
            {
                switch (case_type)
                    {
                        case '0':
                            one.wish();
                        case '1':
                            two.wish();
                        case '2': 
                            three.wish();
                    }
            }
        case '2':
            {
                switch (case_type)
                    {
                        case '0':
                            one.help();
                        case '1':
                            two.help();
                        case '2': 
                            three.help();
                    }
            }
        case '3':
            {
                x = false;
            }
        }
        }
    }

int main()
{
    fairy game;
    good one;
    evil two;
    equivocal three;
    string NAME;
    int case_type = 0;
    int num = rand();
    for (int i = 0; i < 3; i++)
    {
    switch (num % 3)
        {
            case '0':
                NAME = one.name;
                case_type = 0;
            case '1':
                NAME = two.name;
                case_type = 1;
            case '2': 
                NAME = three.name;
                case_type = 2;
            default:
                case_type = -1;
        }

    game.intro(NAME, case_type);
    num++;
    }


}

fairy::call_decision在所有派生类之后定义是否可以?(这允许我在派生类的基类中进行引用。)

如果是这样,为什么我在尝试编译时会出现 lnk 2001 错误?

如果没有,我怎样才能让它在void fairy::call_decision定义之后立即使用void fairy::intro定义进行编译?

4

2 回答 2

2

您遇到问题的原因是因为您都已std::string name声明static。您必须static在命名空间范围内定义类定义之外的成员,如下所示:string good::name = "Sandy";

于 2012-05-23T00:20:27.710 回答
1

大多数编译错误是因为您没有包含string类型或cout. 将以下内容放在文件顶部:

#include <iostream>
#include <string>
using namespace std;
于 2012-05-23T00:13:31.890 回答