passing vector from one class to another

Pages: 12
sorry for that *iter.

I removed it, I get no error. but no out put too. how can i be sure that BidList has been copied to list2?
I suspect what you really want to do is have list being the input parameter like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Auctioneer::accept_bids(const BidList& list){    
    BidList list2;
    BidList::const_iterator iter;
    copy (list.begin(),list.end(), back_inserter(list2));

    for(iter=list2.begin(); iter != list2.end(); iter++)
    {
    	const Bid& bid = *iter; // Get a reference to the Bid object that the iterator points to
    	cout << "Bid id  : " << bid.bidId << endl;
    	cout << "Trd id  : " << bid.trdId << endl;
    	cout << "Quantity: " << bid.qty << endl;
    	cout << "Price   : " << bid.price << endl;
    	cout << "Type    : " << bid.type << endl;
    }
    cout << *iter;
}


Last edited on
Well, am simply trying to copy a vector from the Trader class, I have the function just as above except for th *iter-"removed". I get no out put.So I can know wether the bids from the trader got to the auctioneer.
Can anyone help me spot why my program is not returning any values.Is my call to the Vector wrong?? I try to call the vector from the base class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream> 
#include <vector> 
#include <string> 
#include <algorithm> 
#include <cstdlib>
#include <iomanip>   
 
using namespace std; 
 
const int NUMSELLER = 1; 
const int NUMBUYER = 1; 
const int NUMBIDS = 20; 
const int MINQUANTITY = 1; 
const int MAXQUANTITY = 30; 
const int MINPRICE =100; 
const int MAXPRICE = 150; 
int s=0;
int trdId;
 
// Bid, simple container for values 
struct Bid {  
        int bidId, trdId, qty, price;   
        char type; 
         
        // for sort and find.   
        bool operator<(const Bid &other) const { return price < other.price; }  
        bool operator==(int bidId) const { return this->bidId == bidId; }  
};  
 
// alias to the list, make type consistent 
typedef vector<Bid> BidList;
 
// this class generates bids! 
class Trader {  
private:  
        int nextBidId; 
 
public:  
        Trader(); 
        Bid getNextBid(); 
        Bid getNextBid(char type); 
        // generate a number of bids 
        void loadRange(BidList &, int size); 
        void loadRange(BidList &, char type, int size);
};  
 
Trader::Trader() : nextBidId(1) {} 
 
#define RAND_RANGE(min, max) ((rand() % (max-min+1)) + min) 
         
Bid Trader::getNextBid() { 
        char type = RAND_RANGE('A','B');  
        return getNextBid(type); 
} 
 
Bid Trader::getNextBid(char type) {
        for(int i = 0; i < NUMSELLER+NUMBUYER; i++)
     {                 
       // int trdId = RAND_RANGE(1,9); 
        if (s<10){trdId=0;type='A';}
        else {trdId=1;type='B';}
        s++;
        int qty = RAND_RANGE(MINQUANTITY, MAXQUANTITY);  
        int price = RAND_RANGE(MINPRICE, MAXPRICE); 
        Bid bid = {nextBidId++, trdId, qty, price, type}; 
        return bid; 
} 
}
void Trader::loadRange(BidList &list, int size) { 
        for (int i=0; i<size; i++) { list.push_back(getNextBid()); } 
} 
 
void Trader::loadRange(BidList &list, char type, int size) { 
        for (int i=0; i<size; i++) { list.push_back(getNextBid(type)); } 
}

//---------------------------AUCTIONEER-------------------------------------------

class Auctioneer {

vector<Auctioneer> List;
Trader trader;
vector<Bid> list;
public:
    Auctioneer(){cout<<"heloo\n";};
    void accept_bids(const BidList& bid); 
   
};

typedef vector<Auctioneer> bidlist;
                                                                 
void Auctioneer::accept_bids(const BidList& bidlist)
{ 
 
    for (BidList::const_iterator iter = bidlist.begin(); 
                                 iter != bidlist.end(); 
                                 ++iter) 
    { 
     const Bid& bid = *iter; // Get a reference to the Bid object that the iterator points to 
     cout << "Bid id  : " << bid.bidId << endl; 
     cout << "Trd id  : " << bid.trdId << endl; 
     cout << "Quantity: " << bid.qty << endl; 
     cout << "Price   : " << bid.price << endl; 
     cout << "Type    : " << bid.type << endl; 
    } 
} 


void Simulator::run()
	{ 
      auctioneer.accept_bids(trader.loadRange(bidlist));//receiver
      //auctioneer.displayBids();    // print from receiver
    }
   
Simulator::accept_bids(bid_vector::const_iterator begin, bid_vector::const_iterator end))
all the happy display commands 
void show(const Bid &bid) {  
 cout << "\tBid\t(" <<  setw(3) << bid.bidId << "\t " << setw(3) << bid.trdId  << "\t " 
     << setw(3) <<  bid.type <<"\t " << setw(3) << bid.qty <<"\t "  << setw(3) << bid.price <<")\t\n "  ;    
}  
 

int main(int argc, char **argv) {  
         Trader trader; 
         BidList list;  
         Auctioneer auctioneer;
         //bidlist list;
         auctioneer.accept_bids(list);

         system("pause");
        return 0;  
}
You seem to be missing the class declaration for Simulator.
Also, in your main function you don't actually put any bids in so that may be why you are not getting any out.
Simulator was to be commented off before posting, sorry I forgot to.But its not the problem. Probably I need a way of makiing the bids stick into the vecotor. The bids are auto generated. then packed into BidList, I call bidList by refrence from Austioneer, and copy the bids to another storage. however at the destination, I find nothing in the container!
In your function void Auctioneer::accept_bids(const BidList& bidlist) you don't actually copy any bids, you simply print them out.

I though this was for making a copy to lisdt 2.

1
2
copy (list.begin(),list.end(), back_inserter(list2));

Otherwise how can I make the copy? I intend to call this function in the constructor of Auctioneer.
That line was simply copying the data into another local list rather than the list you declared for the whole object;

Maybe you meant to to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

void Auctioneer::accept_bids(const BidList& bidlist)
{ 
    // Note copy from the input bidlist to the object's list declared at line 83 above 
    copy (bidlist.begin(),bidlist.end(), back_inserter(list)); 

    for (BidList::const_iterator iter = bidlist.begin(); 
                                 iter != bidlist.end(); 
                                 ++iter) 
    { 
     const Bid& bid = *iter; // Get a reference to the Bid object that the iterator points to 
     cout << "Bid id  : " << bid.bidId << endl; 
     cout << "Trd id  : " << bid.trdId << endl; 
     cout << "Quantity: " << bid.qty << endl; 
     cout << "Price   : " << bid.price << endl; 
     cout << "Type    : " << bid.type << endl; 
    } 
} 

Last edited on
Thanks a lot for the efforts and time!
Topic archived. No new replies allowed.
Pages: 12