tags:

views:

89

answers:

5

I am passing a vector of bids from Trader class to Simulator class.which class then passes it on to the auctioneer class.something seems messed up, can anyone spot it please.

Below is part of the code: Error: 199 expected primary-expression before '&' token

#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);

        void loadRange(BidList &, int size); // generate a number of bids
        void loadRange(BidList &, char type, int size);
        void submitBids();
};

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++)
     {
        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)); }
}

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); }
//-----------------------------------------------------------------

//To go through the bidlist (after sorting) in reverse direction.
//If the first entry  found  is an "A", ignore it
//Look for the first "A" to match it, and so on..........
//If "B" quantity is greater than the matching "A" quantity, copy the "A"
//To matchedBids, copy the "B" to matchedBids but with  reduced quantity equal to the "A"
//REPLACE the "B" in the original vector with an NEW "B"


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

class Auctioneer {

public:

Auctioneer (const BidList& vec);   // copy constructor

typedef vector<Auctioneer> vec;
typedef vector<Auctioneer> buyers;
typedef vector<Auctioneer> sellers;
typedef Auctioneer* iterator;
typedef const Auctioneer* const_iterator;
//typedef size;
typedef Auctioneer value_type;

vector<Bid> list;
//typedef vector<Bid> BidList;
//typedef vector<Bid> iterator = BidList;

void accept_bids(vector<Bid> lst) {list = lst;}
void storeBids(){copy(list.begin(), list.end(),
                 bids.begin());}
void displayBids(){cout << "Ok the bids" << endl;} ;

void matchBid();
void calculateProfit();
vector<Auctioneer>::const_iterator b, e;
// new functions to return iterators
iterator begin() { return data; }
const_iterator begin() const { return data; }

iterator end() { return limit; }
const_iterator end() const { return limit; }

private:
string bids;
iterator data;
iterator limit;

};
//---------------------------------SIMULATOR-------------------------------------------
class Simulator {
    Trader trader;
    Auctioneer auctioneer;
    //SmartTrader  strader;

public:
   vector<Bid> list;
   char type;

   void run();
};

void Simulator::run()
    {
      trader.loadRange(BidList &list); //calling from Base class
      auctioneer.receiveBids(list);//receiver
      auctioneer.displayBids;    // print from receiver
    }

//Simulator::accept_bids(bid_vector::const_iterator begin, bid_vector::const_iterator end))


int main() {
       // Trader Trader;
//        BidList bidlist;
         Auctioneer auctioneer;

        auctioneer.loadRange(bidlist, NUMBIDS);
        show("Bids before sort:", bidlist);
        sort(bidlist);
        show("Bids after sort:", bidlist);

        //count(bidlist);

        //unpair(bidlist);
        //unpair("Bids after sort:", bidlist);

        searchTest(bidlist, 3);
        searchTest(bidlist, 33);
         system("pause");
        return 0;
}
+1  A: 

Add

#include <vector>

And either put

using namespace std;

at the top of your file or, preferably, change your vectors to

std::vector

If thats not it .. then we need to know what line the error falls on.

Goz
`using namespace` is most probably bad advice since that code probably comes from a header file.
Konrad Rudolph
I know ... I'll add a preferably ;)
Goz
The line is 199.Error: expected primary expression before const.I have namespace std.
wilson88
A: 

You need to include <vector> and you need to fully qualify the name of the vector class in every usage, e.g.:

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

Furthermore, you should probably pass this reference as const:

void loadRange(std::vector<Bid> cont& bids) {}
Konrad Rudolph
wilson88
A: 

It might not be recognizing BidList. Make sure it's either defined or included in a header file at the top, or at the very least put class BidList at the top. You really do need to show us which line the error is referring to, though.

Kyle
I have edited the code for a clear guidance.I have typedef vector<Bid> BidList; just after the strut. I generate the bids, but the trader is still stack with the container of bids.
wilson88
A: 

It's very suspicious that you have a BidList type, but use a vector everywhere else. Considering that you use it in one location as a reference, I'm going to guess that BidList is not defined.

RickNotFred
BidList is defined, however.I am creating new vectors in the simulator class which will hold bids separately.One for buyers and another for sellers
wilson88
A: 

Your less function is insufficient. Given two bids A=(bID=3, tID=4, $2.4, 20000) and B=(bID=4,tID=11, $2.4, 20000) A<B returns false and B<A returns false. That is broken. Change it to:

bool operator<(const Bid& other) {
      if (price < other.price) return true;
      return bidID < other.bidID; // assuming bidID is unique, otherwise add trade id to the mix
}

This could cause some container operations to be screwed up. compareBidList() has the same issue.

jmucchiello
Am commenting off that part of the code,the task is transfering the bids to the auctioneer.I hope to look into comparison on second stage. it was just for testing.
wilson88
You pass compareBidList() to the std::sort as part of the main() so I assume you want it to work correctly.
jmucchiello