开发者

how to pass vector objects from one class to another

This is how I had tried sending the vector using one of the answers I got., but has not worked out yet.Should copy whn it gets to auctioneer.I want to display.

class Trader {   
private:   
        int nextBidId; 

public:   

        void loadRange( vector <Bid> & bids ) {} ;

class Simulator //Receives from the Trader,doent modify 
{ 
    vector <Bid> bids;
    Trader trader; 
    Auctioneer auctioneer; 

public: 
    void run(); 
}; 

void Simulator::run() { 
    trader.loadRange(vector<Bid> & bids);
    auctioneer.accept_bids(bid_vector::const_iterator begin, bid_vector::const_iterator end);
    auctioneer.displayBids();
} 

class Auctioneer // Has to receive from the simulator class 
{ 

public: 
    vector <Bid> bids,v2;
    void accept_bids(vector<Bid> & bids);
    void displayBids(){cout << "\tBid\t(" <<  s开发者_如何学JAVAetw(3) << bids.bidId << "\t " << setw(3) << bids.trdId  << "\t "  
                                          << setw(3) <<  bids.type <<"\t " << setw(3) << bids.qty <<"\t "  << setw(3) 
                                          << bids.price <<")\t\n "  ;    }
}; 

UPDATE

I have just copied a vector from another class, am now trying to see its contednts.its returning an error:

Error: - begin has not been declared.how do I decalre bigin for the vector?

void Auctioneer::accept_bids(const BidList& bid){ 
    vector<Auctioneer> *list;
    vector<Auctioneer>::iterator itr; // create an iterator

     for ( itr = list.begin();
     itr != list.end(); ++itr )
     cout << *itr << ' ';
}


Option 1: Just pass a copy of the vector

Simply pass the vector into a function in class B (e.g. B::accept_bids(const bid_vector& bids), or perhaps using iterators B::accept_bids(bid_vector::const_iterator begin, bid_vector::const_iterator end)), and have that function copy the vector into a private data member with the assignment operator (or std::copy() if you use iterators). This is the simplest and most straightforward solution.

Option 2: Your vector is large, and the costs (allocation time, copy time, memory use) of copying it are high

Pass the vector as above, but instead of taking a copy of the vector, store the reference (or the iterators) and ensure that class A's copy of the original vector does not go away or change after it has been passed to class B.

Alternately, pass it using std::auto_ptr and make it clear (with a function name such as give_bid_vector(std::auto_ptr<bid_vector> bids)) that class B is taking ownership of the vector. This means class A is no longer the owner of the vector and should not delete it.


I'm assuming this is the bit of code you are talking about:

{
    trader.loadRange(bidlist, NUMBIDS); 
    auctioneer.receiveBids(bidlist, NUMBIDS);
    auctioneer.displayBids(string ss){return str;}
}

In which case you need to make the three functions look something like this:

class Trader {
   ...
   void loadRange( vector <Bid> & bids ) {
      // do stuff with bids
   }
};

the other functions look the same. Then modify the calling code:

{
    vector <Bid> bidList;
    trader.loadRange(bidlist ); 
    auctioneer.receiveBids(bidlist);
    auctioneer.displayBids(string ss){return str;}
}

Note there is no need to pass the size around - vectors have their own size() member.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜