在模板类中使用"this"会导致参数初始化错误

Use of "this" in template classes cause error on parameter initialization

本文关键字:参数 错误 初始化 this      更新时间:2023-10-16

我有两个模板类,CardTemplate和Handtemplate,旨在相互配合以完成两者共有的繁重工作。然后,每个都被子类化到MyCard和MyHand,这些类将包含特定于这些子类的代码。模板类背后的想法是,获得一张牌的手会返回(在这种情况下)MyHand,它确实如此。问题出现在第 36 行,该行后面记录了错误。在代码实例化时,"this"是指向CardTemplate的指针,而MyHand的实例化期望参数是MyCard。代码如下:

#include <QCoreApplication> // Only way I can get access to std::vector in Qt
template <typename> class Tile;
template <typename handType>
class CardTemplate
{
public:
CardTemplate() {}
handType* GetHand() { return ourHand; }
void SetHand(handType* hand) { ourHand = hand; }
int  GetOurIndex();
handType* ourHand;
};

template <typename cardType>
class HandTemplate
{
public:
HandTemplate(){}
void AddCard(cardType* card) { ourCards.push_back(card); }
int  GetCardIndex(cardType* card) { return 0; /* do something like ourCards.indexOf(card); (Qt QVector)*/ }
cardType* GetCardAt(int index) { ourCards.at(index); }
//    QVector<cardType*> ourCards;
std::vector<cardType*> ourCards;
};
template <typename handType>
int CardTemplate<handType>::GetOurIndex()
{
return ourHand->GetCardIndex(this);
// Creator says: error: invalid conversion from ‘CardTemplate<MyHand>*’ to ‘MyCard*’ [-fpermissive]
// Clang says:   error: cannot initialize a parameter of type 'MyCard*' with an rvalue of type 'CardTemplate<MyHand>*'
//               note: in instantiation of member function 'CardTemplate<MyHand>::GetOurIndex' requested here
//               note: passing argument to parameter card here
}
class MyHand;
class MyCard : public CardTemplate<MyHand>
{
public:
MyCard() {}
};
class MyHand : public HandTemplate<MyCard>
{
public:
MyHand() {}
};
int main(void)
{
MyHand aHand;
MyCard aCard;
aHand.AddCard(&aCard);
aCard.SetHand(&aHand);
aCard.GetHand();
int index = aCard.GetOurIndex();
return 0;
}

有什么想法吗?

问题是您正在传递CardTemplate<MyHand>的实例int GetCardIndex(cardType* card) { return 0;,您必须传递一个MyCart实例。解决此问题的一种方法是更改方法签名以获取CardTemplate<MyHand>实例。

  1. 在 CardTemplate 类中添加using T = handType,如下所示:

    template <typename handType>
    class CardTemplate
    {
    public:
    using T = handType;
    CardTemplate() {}
    handType* GetHand() { return ourHand; }
    void SetHand(handType* hand) { ourHand = hand; }
    int  GetOurIndex();
    handType* ourHand;
    };
    
  2. GetCardIndex()方法签名更改为获取和CardTemplate<MyHand>实例,如下所示:

    template <typename cardType>
    class HandTemplate
    {
    public:
    HandTemplate(){}
    void AddCard(cardType* card) { ourCards.push_back(card); }
    int  GetCardIndex(CardTemplate<typename cardType::T>& card){ (void)card; return 0; }
    cardType* GetCardAt(int index) { ourCards.at(index); }
    std::vector<cardType*> ourCards;
    };