在模板类中使用"this"会导致参数初始化错误
Use of "this" in template classes cause error on parameter initialization
我有两个模板类,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>
实例。
-
在 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; };
-
将
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; };
相关文章:
- C++声明模板参数阴影模板参数错误
- 为什么 CRTP 模板C++给出无效参数错误?
- (C/C++)fscanf_s从txt文件以字符形式读取数组时缺少整数参数错误
- 参数错误可能与类型不匹配有关?
- printf 和 strftime 的参数错误无效
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 如何修复"没有重载函数需要 2 个参数"错误C++
- 术语不计算为函数采用 1 个参数错误?
- 从带有 getline() 的文件读入一行上有多个信息得到无效参数错误
- 为什么我不能像使用 std::string::size_type 那样使用 QList::size_type?(模板参数错误)
- cudaFreeHost() 无效参数错误
- 精神语法不会编译:函数模板参数错误?
- C 功能具有参考参数错误的迭代器错误.寻求解释
- 使用getDefaultCommConfig使用无效的参数错误
- C++ 可变参数模板和模板模板参数:错误:模板参数列表中参数 1 处的类型/值不匹配
- 使用SWIG生成的Python库时,向量分配器参数错误
- 错误C2664:无法转换参数错误
- C 作为参数错误的功能
- 命令行参数错误
- 增强Python参数错误