C++;重载继承不编译? 我正在用C++做扑克游戏,我只是想开始。 我需要比较“手”的能力,看哪只手更大、相等或更小。 所以,我现在有一个手工类,我创建了另外两个子类,称为Straight和Threeof Kind(我稍后会添加其余的)。 Hand类有一个名为compareTo(Hand*otherHand)的方法,然后它检查手的排名,看哪一个更好。另外,对于直线和a类中的三个,当它们的排名相同时,可以将它们进行比较。就像直线和直线以及a类中的三个和a类中的三个一样

C++;重载继承不编译? 我正在用C++做扑克游戏,我只是想开始。 我需要比较“手”的能力,看哪只手更大、相等或更小。 所以,我现在有一个手工类,我创建了另外两个子类,称为Straight和Threeof Kind(我稍后会添加其余的)。 Hand类有一个名为compareTo(Hand*otherHand)的方法,然后它检查手的排名,看哪一个更好。另外,对于直线和a类中的三个,当它们的排名相同时,可以将它们进行比较。就像直线和直线以及a类中的三个和a类中的三个一样,c++,inheritance,overloading,poker,C++,Inheritance,Overloading,Poker,我今天写了一些初始代码,我的问题是,当我试图调用“Hand”的compareTo(Hand*otherHand)方法并传递一个Hand、direct或三个类似的方法时,编译器会抱怨,因为它试图强迫我使用direct的compareTo(direct*otherdirect)方法。因此,我应该使用重载,但它不起作用 所以在继承完成后的Straight类中,我们应该有以下两种方法: int Hand::compareTo(Hand* otherHand); int Straight::compare

我今天写了一些初始代码,我的问题是,当我试图调用“Hand”的
compareTo(Hand*otherHand)
方法并传递一个Hand、direct或三个类似的方法时,编译器会抱怨,因为它试图强迫我使用direct的
compareTo(direct*otherdirect)
方法。因此,我应该使用重载,但它不起作用

所以在继承完成后的Straight类中,我们应该有以下两种方法:

int Hand::compareTo(Hand* otherHand);
int Straight::compareTo(Straight* otherStraight);

// But, if you do this, it works:
Straight myStraight1 = new Straight(7);
Straight myStraight2 = new Straight(5);
myStraight1.compareTo(myStraight2);
// This is valid...

// If you do this, then the compiler complains!
Straight myStraight3 = new Straight(10);
ThreeOfKind myTrips4 = new ThreeOfKind(3);
myStraight3.compareTo(myTrips4);
// This above line complains that you cannot convert a ThreeOfKind to a Straight
// Even though I am trying to use Hand's compareTo(Hand* otherHand) method and
// cast a Three of a Kind to a Hand object,
// it fails with the overloading!
这里是所有的源代码

//////////////////////////
// C++ main header file //
//////////////////////////


#pragma once


class Hand {

private:
    int ranking;

public:
    Hand(int aRanking);
    Hand();

    int getRanking();

    int compareTo(Hand* otherHand);
};

class Straight : public Hand {

private:
    int highCard;

public:

    Straight(int aHighCard);
    Straight();

    int getHighCard();

    int compareTo(Straight* otherStraight);
};

class ThreeOfKind : public Hand {

private:
    int tripsValue;

public:

    ThreeOfKind(int aTripsValue);
    ThreeOfKind();

    int getTripsValue();

    int compareTo(ThreeOfKind* otherThreeOfKind);
};


///////////////////////////
// C++ main .cpp file... //
///////////////////////////
#include <iostream>
#include "PokerTest1.h"
using namespace std;

Hand::Hand(int aRanking) {

    this->ranking = aRanking;
}

Hand::Hand() {

    this->ranking = 0;
}

int Hand::getRanking() {
    return this->ranking;
}

int Hand::compareTo(Hand* otherHand) {

    cout << "COMPARING HANDS..." << endl;

    if (this->getRanking() < otherHand->getRanking()) {

        cout << "HANDS RETURNING -1..." << endl;

        return -1;
    }
    else if (this->getRanking() > otherHand->getRanking()) {

        cout << "HANDS RETURNING 1..." << endl;

        return 1;
    }

    cout << "HAND RANKINGS ARE EQUAL..." << endl;

    if (this->getRanking() == 4 && otherHand->getRanking() == 4) {

        cout << "HANDS ARE BOTH STRAIGHTS..." << endl;

        Straight* myStraight1 = (Straight*)this;
        Straight* myStraight2 = (Straight*)otherHand;

        cout << "COMPARING BOTH STRAIGHTS..." << endl;

        return myStraight1->compareTo(myStraight2);
    }
    else if (this->getRanking() == 3 && otherHand->getRanking() == 3) {

        cout << "HANDS ARE BOTH THREE OF A KINDS..." << endl;

        ThreeOfKind* myTrips1 = (ThreeOfKind*)this;
        ThreeOfKind* myTrips2 = (ThreeOfKind*)otherHand;

        cout << "COMPARING BOTH TRIPS..." << endl;

        return myTrips1->compareTo(myTrips2);
    }

    return 0;
}

Straight::Straight(int aHighCard) : Hand(4) {
    this->highCard = aHighCard;
}

Straight::Straight() : Hand(4) {
    this->highCard = 0;
}

int Straight::getHighCard() {
    return this->highCard;
}


int Straight::compareTo(Straight* otherStraight) {

    cout << "INSIDE STRAIGHT COMPARE TO..." << endl;

    if (this->highCard < otherStraight->highCard) {

        cout << "STRAIGHT COMPARE RETURNING -1..." << endl;

        return -1;
}
    else if (this->highCard > otherStraight->highCard) {

        cout << "STRAIGHT COMPARE RETURNING 1..." << endl;

        return 1;
    }

    cout << "STRAIGHT COMPARE RETURNING 0..." << endl;

    return 0;
}


ThreeOfKind::ThreeOfKind(int aTripsValue) : Hand(3) {
    this->tripsValue = aTripsValue;
}

ThreeOfKind::ThreeOfKind() : Hand(3) {
    this->tripsValue = 0;
}

int ThreeOfKind::getTripsValue() {
    return this->tripsValue;
}

int ThreeOfKind::compareTo(ThreeOfKind* otherThreeOfKind) {

    cout << "INSIDE STRAIGHT COMPARE TO..." << endl;

    if (this->tripsValue < otherThreeOfKind->tripsValue) {

        cout << "TRIPS COMPARE RETURNING -1..." << endl;

        return -1;
    }
    else if (this->tripsValue > otherThreeOfKind->tripsValue) {

        cout << "TRIPS COMPARE RETURNING 1..." << endl;

        return 1;
    }

    cout << "TRIPS COMPARE RETURNIN 0..." << endl;

    return 0;
}

int main()
{

    // Test the classes...
    // with Straight compared to a Three of a Kind.
    // Should try to invoke Hand::compareTo(Hand* otherHand) { ... };
    // But, instead, it try to invoke Straight::compareTo(Straight* otherStraight) { ... };

    // If you put both these methods in the Straight class (rather than using inheritence, it works)
    // If  you delete Straight::compareTo(Straight* otherStraight) { ... }, the line below compiles

    // It is just strange why it won't compile...
    Straight* myStraightA = new Straight(9); // Straight of 5, 6, 7, 8, 9
    ThreeOfKind* myTripsB = new ThreeOfKind(2); // Three of a Kind of 2, 2, 2

    cout << "Compare results..." << endl;
    cout << myStraightA->compareTo(myTripsB) << endl; // Compiler error...

    return 0;
}
基本上,我在Hand类中有一个字段,它将这些排名存储为整数

最后,这是编译器错误消息:

错误C2664:'int Straight::compareTo(Straight)':无法将参数1从'ThreeOfKind'转换为'Straight*'

编译器会查找
compareTo
方法,在
direct
中找到它,而不查看
Hand
。如果使用
语句添加适当的
,则可以告诉它查看
Hand
的compareTo,也可以完成重载

class Straight : public Hand {
private:
    int highCard;

public:
    using Hand::compareTo; // <<< HERE

    Straight(int aHighCard);
    Straight();

    int getHighCard();

    int compareTo(Straight* otherStraight);
};
这简化了Hand的比较方式:

int Hand::compareTo(Hand* otherHand) {
    if (this->getRanking() < otherHand->getRanking()) {
        return -1;
    }
    else if (this->getRanking() > otherHand->getRanking()) {
        return 1;
    }

    if (this->getTieBreaker() < otherHand->getTieBreaker()) {
        return -1;
    }
    else if (this->getTieBreaker() > otherHand->getTieBreaker()) {
        return 1;
    }
    return 0;
}

Straight myStraight3=新直线(10)
应该无法编译您的问题,在代码中,类和子类并不是很好的建模方法。如果您有一堆卡片,您必须首先确定这是什么类型的手,然后实例化一个合适类型的对象,这有点奇怪,特别是当您可以绘制或更改卡片,从而“更改对象类型"。此外,用于比较不同人手的代码将杂乱无章地分布在多个类中,这使您很难了解发生了什么。此代码比需要的复杂得多,并且随着您尝试向代码中添加更多案例,此类问题将越来越频繁。包括ode>在每个派生类中使用Hand::compareTo;
将修复编译器错误,但这是在给自己挖一个深洞
class Hand {
public:
    int getRanking();
    // virtual causes subclass' version to be called if done from reference or pointer.
    virtual int getTieBreaker();  
};
int Hand::compareTo(Hand* otherHand) {
    if (this->getRanking() < otherHand->getRanking()) {
        return -1;
    }
    else if (this->getRanking() > otherHand->getRanking()) {
        return 1;
    }

    if (this->getTieBreaker() < otherHand->getTieBreaker()) {
        return -1;
    }
    else if (this->getTieBreaker() > otherHand->getTieBreaker()) {
        return 1;
    }
    return 0;
}
class Straight : public Hand {
//...
public:
    int getHighCard();
    int getTieBreaker();
};

int Straight::getTieBreaker() {
    return this->highCard;
}