c++
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
?
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. eg
Bid highestBid = 0;
BOOST_FOREACH (Bid& bid, bids_)
if (bid.GetAuction()->GetName() == auction->GetName())
highestBid = &bid;
链接地址: http://www.djcxy.com/p/47352.html
上一篇: STL的样板类型定义
下一篇: C ++