迭代通过指针的向量 [英] Iterating through a vector of pointers
本文介绍了迭代通过指针的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图重复通过玩家手牌。
I'm trying to iterate through a Players hand of cards.
Player.cpp
vector<Card*>::iterator iter;
for(iter = current_cards.begin(); iter != current_cards.end(); iter++) {
cout << iter->display_card() << endl;
}
cout << iter->display_card() << endl;
目前出现的错误:表达式必须具有指针类类型。
currently comes up with the "error: Expression must have pointer-to-class type".
同样,current_cards用以下声明:
Likewise, current_cards is declared with:
vector<Card*>current_cards;
此外,display_card()方法简单:
Furthermore, the display_card() method is simply:
Card.cpp
string Card::display_card(){
stringstream s_card_details;
s_card_details << "Colour: " << card_colour << "\n";
s_card_details << "Type: " << card_type << "\n";
return s_card_details.str();
}
我查看了各种资源,问题没有为我工作。非常感谢您的帮助!
I have looked over various resources and everything that has been suggested for similar types of issues hasn't worked for me. Thanks for any help!
推荐答案
尝试:
cout << (*iter)->display_card() << endl;
*
由迭代器,在你的情况下是一个指针。然后使用 - >
解除引用该指针。
The *
operator gives you the item referenced by the iterator, which in your case is a pointer. Then you use the ->
to dereference that pointer.
这篇关于迭代通过指针的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文