tags:

views:

313

answers:

2

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(" <<  setw(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 << ' ';
}
A: 

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.

anon
I tried it that way too, got an error no match for 'operator=' in 'ss = ((Auctioneer*)this)->Auctioneer::bidList' [code]vector <Bid> bidList; void receiveBids(string ss){ss = bidList;}; void displayBids(){ cout << bid.bidId,..... ; }[/code]
wilson88
thanks for the assistance, it worked out so well with solution 2
wilson88
+1  A: 

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.

Matt Curtis
An advantage of passing by iterators is you can copy with a predicate only bids that the second class is interested in, or you can process the vector in a lazy fashion in cases when you might not need to process it all.
Matt Curtis
If you are passing using references, you are not passing a copy.
anon
You're right, but I can't find where I said that. Option 1 is about B taking a copy, Option 2 is about B sharing the vector with A.
Matt Curtis