开发者

transferring a container of data to different classes

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 A开发者_StackOverflowuctioneer 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;
}


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.


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜