passing vector from one class to another

Pages: 12
I am still trying to pass the vector from the Base class to middle class which has to pass it to a final class.

Error: expected primary-expression before '&' token

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

class Trader { 
void loadRange(BidList &, int size);// packs the vector with bids

//----------------------------------------------------------------
class Simulator {
   Trader trader;
   Auctioneer auctioneer;
   
public:
   vector<Bid> list;
   char type; 
   void run();{

      trader.loadRange(BidList &list); //calling from Base class 
      auctioneer.receiveBids(list);//receiver
      auctioneer.displayBids;    // print from receiver
     }
		
};
//---------------------------------------------------------
class Auctioneer {
vector<Bid> list;
typedef vector<Auctioneer>bids; 

public:
  void accept_bids(vector<Bid> lst) {list = lst;}     
  void storeBids(){copy(list.begin(), list.end(),
                   bids.begin());}  
void displayBids(){cout << ......} ; 

Line 3 & 15 are a little odd.

Should they be:

 
void loadRange(vector<Bid>& list, int size);// packs the vector with bids 


and:

 
trader.loadRange(vector<Bid>& list); //calling from Base class  

This is just part of the complete code:I have a
typedef vector<Bid> BidList to make type consistent.
thats why I have

loadRange(BidList &, int size) and

trader.loadRange(BidList &, const list) //which I modified with cost.

But the error still remains the same
There needs to be some variable name (local) after the & if you're going to make your prototypes like that.

Maybe not...

-Albatross
Last edited on
I try to show u whts going on.
The trader loads the bids.
1
2
3
void Trader::loadRange(BidList &list, int size) { 
        for (int i=0; i<size; i++) { list.push_back(getNextBid()); } 
}


The auctioneer receives the container.
1
2
3
4
5
6
7
8
9
10
11
12
13
//declared vector to store after receiving
typedef vector<Auctioneer> vec;
vector<Auctioneer>list2;

void accept_bids(){loadRange(BidList list2 cont& list);//did you mean list2?
//trying to copy them to vec if received

copy(BidList list.begin(), BidList.end(),vec);
}  

Error: Primary expression required before list2;

typedef makes sense.

Line 13 looks wrong void run();{

Should that ; be there?
I take it that Simulator and Auctioneer are meant to be inner classes of Trader??
well that was a typo. this is the adjusted code.


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

class Trader { 
void loadRange(BidList &, int size);// packs the vector with bids

//----------------------------------------------------------------
class Simulator {
   Trader trader;
   Auctioneer auctioneer;
   
public:
   vector<Bid> list;
   char type; 
   void run();{

      auctioneer.receiveBids( trader.loadRange(BidList &list););//receiver
      auctioneer.displayBids;    // print from receiver
     }
		
};
//---------------------------------------------------------
class Auctioneer {
vector<Bid> list;
typedef vector<Auctioneer>bids; 

public:
  void receiveBids(BidList &lst) {list = lst;}     
  void storeBids(){copy(list.begin(), list.end(),bids.begin());}  
void displayBids(){cout << ......} ; 
Line 13 is still wrong.
I understand I simply need a function to call the vector by refrence and store the contents in the new vector.I tried it this way but did work for me.anyone with any opinion?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Auctioneer {

vector<Auctioneer> List;

public:

void accept_bids(const BidList& bids); 
};
typedef vector<Auctioneer> List;
                                                                 
void Auctioneer::accept_bids(const BidList& bids){   
vector<Auctioneer> List;

copy (Bidlist.begin(),BidList.end(), List);// tried copying to vector List!
}

int main()
{
Auctioneer auctioneer;

auctioneer.accept_bids(Bidlist);//tried displaying before copying
return 0;
}
@gnwillix88

Can you post your whole code in one go?
I think
copy (Bidlist.begin(),BidList.end(), back_inserter(List));// tried copying to vector List!
is better.
Still geting an error.The all code is as below:
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#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);
        void setVector();
};  
 
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(){};
    void accept_bids(const BidList& bid); 
};

typedef vector<Auctioneer> bidlist;
                                                                 
void Auctioneer::accept_bids(const BidList& bid){    
    BidList list;
     BidList list2;
    BidList::const_iterator iter;
    copy (BidList.begin(),BidList.end(), back_inserter(list2));
    //copy (BidList.begin(),BidList.end(),list2); 
    for(iter=list2.begin(); iter != list2.end(); iter++)//{     
    cout << iter << endl<<"\n";
         
}

//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 "  ;    
}  
  
void show(const BidList &list) {  
        cout << "\t\tBidID | TradID | Type  | Qty  |  Price  \n\n";   
        for(BidList::const_iterator itr=list.begin(); itr != list.end(); ++itr) {  
                //cout <<"\t\t";  
                show(*itr); 
                cout << endl;  
        }  
        cout << endl;  
} 

//search now checks for failure 
void show(const char *msg, const BidList &list) {  
        cout << msg << endl; 
        show(list); 
} 

void searchTest(BidList &list, int bidId) {  
        cout << "Searching for Bid " << bidId << endl; 
        BidList::const_iterator itr = find(list.begin(), list.end(), bidId);  
        if (itr==list.end()) { 
                cout << "Bid not found.";  
        } else { 
                cout << "Bid has been found. Its : ";  
                show(*itr); 
        } 
        cout << endl;  
}  
 
//comparator function for price: returns true when x belongs before y 

bool compareBidList(Bid one, Bid two) { 
 
        if (one.type == 'A' && two.type == 'B') 
                return (one.price < two.price); 
 
        return false; 
} 
 
void sort(BidList &bidlist) { sort(bidlist.begin(), bidlist.end(), compareBidList); }  


int main(int argc, char **argv) {  
         Trader trader; 
         BidList bidlist;  
         Auctioneer auctioneer;
         //bidlist list;
         auctioneer.accept_bids(bidlist);
         //trader.loadRange(bidlist, NUMBIDS); 
         show("Bids before sort:", bidlist);  
         sort(bidlist);  
         show("Bids after sort:", bidlist);  
 
         system("pause");
        return 0;  
}
This bit here, starting at Line 92:
1
2
3
4
5
6
7
8
9
10
void Auctioneer::accept_bids(const BidList& bid){    
    BidList list;
     BidList list2;
    BidList::const_iterator iter;
    copy (BidList.begin(),BidList.end(), back_inserter(list2)); //////////////////<<<<<<
    //copy (BidList.begin(),BidList.end(),list2); 
    for(iter=list2.begin(); iter != list2.end(); iter++)//{     
    cout << iter << endl<<"\n";
         
}


That line (96) is an error because BidList is a type (see line 31) not an object - so the dot (member access) operator
cannot be used like that.
Maybe you meant to use the bid parameter that was passed to the function????
copy (bid.begin(),bid.end(), back_inserter(list2));


I didn't go any further than that.
Last edited on
I corrected that ! and got this error
99 no match for 'operator<<' in 'std::cout << iter'
Line 99 has an error.

You are trying to output an iterator rather than the data it points to.

So you probably mean something like this:

 
cout << *iter << endl <<"\n";


However that also will not work because operator<< does not understand how to output your Bid object.

So you need to overload operator<< or else you need to output its data member's one by one like this:


1
2
3
4
5
6
7
8
9
    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;
    }

Last edited on
I included
Compiler still complaining of the << .No match for the operator.

the revised code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Auctioneer::accept_bids(const BidList& bid){    
    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;
}
You still have this at line 16 above:

 
cout << *iter;


That is what is causing the error. You need to delete that.

Its because in order to say cout << *iter, you need to first tell the << operator how the object of class Bid that *you* created should be handled.

So unless you want to do that by overriding the operator<<() function, you need to output the values individually as in the code above.
And after that you have other problems, logical ones.

In this function you don't do anything with the passed in parameter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Auctioneer::accept_bids(const BidList& bid){    
    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;
}


You declare two empty lists, list and list2, and you do all your operations on them. But they are empty.
Pages: 12