操作数类型不兼容(“ int”和“ const char *”)

时间:2019-02-01 22:00:41

标签: c++ class operator-overloading

使用我的代码,我创建了一个类,该类具有创建微调器的构造函数。微调器具有使它旋转的过程,并将结果发送到landOn。我已经看过类似的问题,但是在尝试重载运算符后,找不到一个提到“ int”与“ const char *”不兼容的问题。

我要重载“ ==”以便将landOn与“ const char *”进行比较的部分,该名称是旋转器将读出的水果的名称:

    bool operator == (Cspinner leftside, const char * rightside)
{
    return leftside.landOn == rightside;
}

据我了解,我无法将landOn声明为“ int”,因为当我尝试比较它们时,它与“ const char *”兼容。

这是完整的代码:

    #include <iostream>
    #include <time.h>
    #include <string>

    using namespace std;

    class Cspinner
    {
        friend bool IsEqual(Cspinner param1, Cspinner param2);
        friend bool operator == (Cspinner leftside, Cspinner rightside);
        friend bool operator == (int leftside, Cspinner rightside);
        friend ostream & operator <<(ostream & leftside, Cspinner rightside);
        friend bool operator == (Cspinner leftside, const char* rightside);
    private:
        int result;
        int landOn = 0;
        int fruit[5] = { 30, 55, 75, 90, 100 };
    public:
        Cspinner()
        {
            fruit[0] = 30; // 30% Probability
            fruit[1] = 55; // 25% Probability
            fruit[2] = 75; // 20% Probability
            fruit[3] = 90; // 15% Probability
            fruit[4] = 100; // 10% Probability
        }

        Cspinner(int Newapple, int Neworange, int Newcherry, int Newbannana, int Newpeach)
        {

            fruit[0] = Newapple;
            fruit[1] = Newapple + Neworange;
            fruit[2] = Newapple + Neworange + Newcherry;
            fruit[3] = Newapple + Neworange + Newcherry + Newbannana;
            fruit[4] = Newapple + Neworange + Newcherry + Newbannana + Newpeach;

        }

        void spin() // Procedure to spin fruit on spinner
        {
            result = rand() % 100;

            if (result <= fruit[0])
            {
                landOn = 0;
            }

            else if (result < fruit[1])
            {
                landOn = 1;
            }

            else if (result < fruit[2])
            {
                landOn = 2;
            }

            else if (result < fruit[3])
            {
                landOn = 3;
            }

            else
            {
                landOn = 4;
            }
        }

        int ReturnlandOn()
        {
            return landOn;
        }

        bool operator ==(int rightside)
        {
            return this->landOn == rightside;
        }

        bool operator < (int rightside)
        {
            return landOn < rightside;
        }


    };

    bool operator == (Cspinner leftside, const char * rightside)
    {
        return leftside.landOn == rightside;
    }

    bool IsEqual(Cspinner param1, Cspinner param2)
    {
        return param1.landOn == param2.landOn;
    }

    bool operator == (Cspinner leftside, Cspinner rightside)
    {
        return leftside.landOn == rightside.landOn;
    }

    bool operator == (int leftside, Cspinner rightside)
    {
        return leftside == rightside.landOn;
    }

    ostream & operator <<(ostream & leftside, Cspinner rightside)
    {
        leftside << rightside.landOn;
        return leftside;
    }

    void main()
    {
        srand(time(NULL));

        Cspinner w1;
        Cspinner w2;
        Cspinner w3(80, 5, 5, 5, 5);


        for (int x = 0; x <= 9; x++)
        {
            w1.spin();
            w2.spin();
            w3.spin();
            cout << w1 << w2 << w3;
            if (w1 == w2 && w2 == w3)
            {
                if (w1 == "Apple")    cout << "  (All Apples) ";
                else if (w1 == "Orange") cout << "  (All Oranges) ";
                else if (w1 == "Cherry") cout << "  (All Cherries) ";
                else if (w1 == "Banana") cout << "  (All Bananas) ";
                else  cout << "  (All Peaches)";
            }

            cout << endl;
        }

        system("pause");
    }

Error: Operand types are incompatible ("int" and "const char*") Line 89

更新

我一直在这一点,似乎是工作,但是我有它的工作的方式可能不是最好的代码:

   #include <iostream>
#include <time.h>
#include <string>

using namespace std;

class Cspinner
{
    friend bool IsEqual(Cspinner param1, Cspinner param2);
    friend bool operator == (Cspinner leftside, Cspinner rightside);
    friend bool operator == (int leftside, Cspinner rightside);
    friend ostream & operator <<(ostream & leftside, Cspinner rightside);
    friend bool operator == (Cspinner leftside, const char* rightside);
private:
    int result;
    int landOn;
    int fruit[5] = { 30, 55, 75, 90, 100 };
    const char * name;
public:
    Cspinner()
    {
        fruit[0] = 30; // 30% Probability
        fruit[1] = 55; // 25% Probability
        fruit[2] = 75; // 20% Probability
        fruit[3] = 90; // 15% Probability
        fruit[4] = 100; // 10% Probability
    }

    Cspinner(int Newapple, int Neworange, int Newcherry, int Newbannana, int Newpeach)
    {

        fruit[0] = Newapple;
        fruit[1] = Newapple + Neworange;
        fruit[2] = Newapple + Neworange + Newcherry;
        fruit[3] = Newapple + Neworange + Newcherry + Newbannana;
        fruit[4] = Newapple + Neworange + Newcherry + Newbannana + Newpeach;

    }

    void spin() // Procedure to spin fruit on spinner
    {
        result = rand() % 100;

        if (result <= fruit[0])
        {
            landOn = 0;
            name = "Apple ";
        }

        else if (result < fruit[1])
        {
            landOn = 1;
            name = "Orange ";
        }

        else if (result < fruit[2])
        {
            landOn = 2;
            name = "Cherry ";
        }

        else if (result < fruit[3])
        {
            landOn = 3;
            name = "Banana ";
        }

        else
        {
            landOn = 4;
            name = "Peach ";
        }
    }

    int ReturnlandOn()
    {
        return landOn;
    }

    bool operator ==(int rightside)
    {
        return this->landOn == rightside;
    }

    bool operator < (int rightside)
    {
        return landOn < rightside;
    }


};

bool operator == (Cspinner leftside, const char * rightside)
{
    return leftside.name == rightside;
}

bool IsEqual(Cspinner param1, Cspinner param2)
{
    return param1.landOn == param2.landOn;
}

bool operator == (Cspinner leftside, Cspinner rightside)
{
    return leftside.landOn == rightside.landOn;
}

bool operator == (int leftside, Cspinner rightside)
{
    return leftside == rightside.landOn;
}

ostream & operator <<(ostream & leftside, Cspinner rightside)
{
    leftside << rightside.name;
    return leftside;
}

void main()
{
    srand(time(NULL));

    Cspinner w1;
    Cspinner w2;
    Cspinner w3(5, 5, 80, 5, 5);

    for (int x = 0; x <= 9; x++)
    {
        w1.spin();
        w2.spin();
        w3.spin();
        cout << w1 << w2 << w3;
        if (w1 == w2 && w2 == w3)
        {
            if (w1 == "Apple ")    cout << "  (All Apples) ";
            else if (w1 == "Orange ") cout << "  (All Oranges) ";
            else if (w1 == "Cherry ") cout << "  (All Cherries) ";
            else if (w1 == "Banana ") cout << "  (All Bananas) ";
            else  cout << "  (All Peaches)";
        }

        cout << endl;
    }

    system("pause");
}

1 个答案:

答案 0 :(得分:0)

对于char *,请使用strcmp(leftside.name, rightside) == 0

编辑:对于名称,为什么不使用std :: string,它将使您的代码更加清晰,并使您更容易。