在 Ptr_Vector 上提升 FOR_EACH?
我目前正在尝试学习一些 Boost 库,这很有趣。我目前正在做我认为未来的作业项目(学期尚未开始)。不过,这道题并不是关于作业的问题,而是关于Boost的。
代码:
/* AuctionApplication.h */
class AuctionApplication : boost::noncopyable
{
private:
boost::ptr_vector<Auction> auctions_;
boost::ptr_vector<Bidder> bidders_;
boost::ptr_vector<Bid> bids_;
/* AuctionApplication.cpp */
Bid *AuctionApplication::GetLatestBid(const Auction *auction)
{
Bid *highestBid = 0;
BOOST_FOREACH(Bid *bid, bids_) // Error here!
if (bid->GetAuction()->GetName() == auction->GetName())
highestBid = bid;
BOOST_FOREACH 用于处理法线向量,代码与上面完全相同。自从我开始使用 ptr_vectors 以来,我收到错误:
error C2440: '=' : Cannot conversion from 'Bid' to 'Bid *'
让我相信 ptr_vector 以某种方式掩盖了 foreach 中的指针方法。
如果我改为编写,
BOOST_FOREACH(Bid *bid, bids_)
我会收到四个类型为
error C2819: type 'Bid' does not have a Heavyed member 'operator ->'
的错误,这很糟糕,因为我知道 bid 是一个指针。
如何使 BOOST_FOREACH
正确迭代 ptr_vectors
?
I'm currently having fun trying to learn some of the Boost libary. I'm currently doing what I guess will be a future homework project (semester hasn't started yet). However, this question is not about the homework problem, but about Boost.
Code:
/* AuctionApplication.h */
class AuctionApplication : boost::noncopyable
{
private:
boost::ptr_vector<Auction> auctions_;
boost::ptr_vector<Bidder> bidders_;
boost::ptr_vector<Bid> bids_;
/* AuctionApplication.cpp */
Bid *AuctionApplication::GetLatestBid(const Auction *auction)
{
Bid *highestBid = 0;
BOOST_FOREACH(Bid *bid, bids_) // Error here!
if (bid->GetAuction()->GetName() == auction->GetName())
highestBid = bid;
BOOST_FOREACH use to work with normal vectors with the exact same code as above. Since I've started using ptr_vectors I get the error:
error C2440: '=' : cannot convert from 'Bid' to 'Bid *'
Leading me to believe that ptr_vector somehow obscures the pointer from the foreach method.
If I instead write
BOOST_FOREACH(Bid *bid, bids_)
I get four errors of the type
error C2819: type 'Bid' does not have an overloaded member 'operator ->'
which sucks, because I know bid is a pointer.
How can I make BOOST_FOREACH
iterate properly over the ptr_vectors
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ptr_vector 获取堆分配对象的所有权并将每个对象呈现为引用,因此您不需要取消引用并使用 .而不是 ->访问成员变量/函数。例如
ptr_vector takes ownership of heap allocated objects and presents each object as a reference so you don't need dereferencing and you use . instead of -> to access member variables/functions. e.g.