c++ - 将数据容器传输到不同的类

标签 c++

我正在将出价 vector 从 Trader 类传递到 Simulator 类。然后哪个类将它传递给拍卖师类。有些东西似乎搞砸了,谁能发现它。

部分代码如下: 错误:199 预期在“&”标记之前的主表达式

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

最佳答案

添加

#include <vector>

要么放

using namespace std;

在你的文件的顶部,或者,最好是,将你的 vector 更改为

std::vector

如果不是这样......那么我们需要知道错误发生在哪一行。

关于c++ - 将数据容器传输到不同的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2830312/

相关文章:

C++ 可变参数模板,参数包长度错误?

c++ - 使用 C++ sql.h 备份 SQL 数据库

c++在类似结构的数组中仅给出字符串的一部分时查找字符串

c++ - 从鼠标左键单击 QTableView 获取行和列?

c++ - 奇怪的行为?

c++ - 如何从命令行使用 Visual Studio 2017 Codegen ClangC2?

c++ - 非常量对象作为具有常量参数的函数的参数 (C++)

c++ - 在英特尔编译器的特定位置禁用警告

c++ - 循环的编译器优化

c++ - 如何让 MS C++ 编译器识别#include 中未定义的类型?