在 Ptr_Vector 上提升 FOR_EACH?

发布于 2024-09-24 11:41:05 字数 1111 浏览 5 评论 0原文

我目前正在尝试学习一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油焖大侠 2024-10-01 11:41:05

ptr_vector 获取堆分配对象的所有权并将每个对象呈现为引用,因此您不需要取消引用并使用 .而不是 ->访问成员变量/函数。例如

Bid highestBid = 0;
BOOST_FOREACH (Bid& bid, bids_)
    if (bid.GetAuction()->GetName() == auction->GetName())
        highestBid = &bid;

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.

Bid highestBid = 0;
BOOST_FOREACH (Bid& bid, bids_)
    if (bid.GetAuction()->GetName() == auction->GetName())
        highestBid = &bid;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文