静态成员的C ++链接器错误

时间:2012-05-23 00:07:15

标签: c++

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进行编译?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

#include <iostream>
#include <string>
using namespace std;