Im trying to access the objects in the vector array that ive created in the bid class. I need to access the first ask bid and then continue to compare the price and the quantity with the first buy bid. I dont understand how on EARTH im supposed access these elements from the simulator.Could someone please help!:(
Im lost at comparing and accessing:S

Heres the code:

#include<iostream>
using namespace std;
class Simulator 
{

vector<Bid> BidList (20);

public:
  
   void run();
   void match();
};

void Simulator::run() 
{
//my bad try at accessing the elements in the vector.completley wrong but i dunno why:s
    for (int i; i<20; i++) 
    {
    theBidlist(BidList);
    BidList[i].show();
    }
    
    cout<<"Bid list after sort:"<<endl;
    sort(BidList.begin(), BidList.end());
    show2(BidList);
    show3(BidList);
}


class Bid
{
private:

   int bidID, quantity, price, traderid;
   char tradertype;
public:
   static int BidID;
   Bid ( int tid, char ttype, int p , int q ) : traderid ( tid ), tradertype ( ttype ), quantity ( p ), price ( q )
   {
      bidID = Bid::BidID++;
   }
//**************************************************

   void show ( ostream &out ) const
   {
      out << "(" << bidID << ",\t" << traderid << ",\t" << tradertype << ",\t" << quantity << ",\t" << price << ")" << endl;
   }

//***************************************
   void show2 ( ostream &out ) const
   {
      if ( tradertype == 'A' )
         out << "(" << bidID << ",\t" << traderid << ",\t" << tradertype << ",\t" << quantity << ",\t" << price << ")" << endl;
   }
//********************************************
   void show3 ( ostream &out ) const
   {
      if ( tradertype == 'B' )
         out << "(" << bidID << ",\t" << traderid << ",\t" << tradertype << ",\t" << quantity << ",\t" << price << ")" << endl;
   }


//********************************************

   bool operator< ( const Bid &other ) const
   {
      return price < other.price;
   }
};

int Bid:: BidID = 0;

//*******************Sets the values for the Bid List*********************************
Bid getBids()
{
   int MinPrice = 50, MaxPrice = 150;
   int range1 = ( MaxPrice - MinPrice ) + 1;
   int MinQuantity = 30, MaxQuantity = 100;
   int range2 = ( MaxQuantity - MinQuantity ) + 1;

   int quantity = MinQuantity + int ( range1 * rand() / ( RAND_MAX + 1.0 ) );
   int price = MinPrice + int ( range2 * rand() / ( RAND_MAX + 1.0 ) );

   int traderid;
   int NUMBIDS = 10;
   char tradertype;
   int bidID = Bid::BidID;
   if ( bidID < NUMBIDS )
   {
      traderid = 0;
      tradertype = 'A';
   }
   else
   {
      traderid = 1;
      tradertype = 'B';
   }

   return Bid ( traderid, tradertype, quantity, price );
}

//*****************************************Adds the random values to the Vector*******************
void theBidlist ( vector<Bid> &list )
{
   int numbids = 20;
   for ( int i = 0;i < numbids;i++ )
   {
      list.push_back ( getBids() );
   }
}

//*******************************************Scans through the vector****************************
void show ( const vector<Bid> &vect )
{
   cout << "********************************************" << endl;

   for ( vector<Bid>::const_iterator sh = vect.begin(); sh != vect.end(); ++sh )
   {
      sh->show ( cout );
   }
   cout << endl;
}


//****************************************************************************************************

void show2 ( const vector<Bid> &vect )
{
   cout << "*******************Only Trader Ask*************************" << endl;

   for ( vector<Bid>::const_iterator sh = vect.begin(); sh != vect.end(); ++sh )
   {
      sh->show2 ( cout );
   }
   cout << endl;
}

//**************************************************************************************************

void show3 ( const vector<Bid> &vect )
{
   cout << "*******************Only Trader Buys*************************" << endl;

   for ( vector<Bid>::const_iterator sh = vect.begin(); sh != vect.end(); ++sh )
   {
      sh->show3 ( cout );
   }
   cout << endl;
}
theBidlist(BidList);
    BidList[i].show();

What sort of issue are you having? Will it compile? If not, what are the errors reported?

Also, what is "theBidList" and where did it come from?

theBidlist is the function in the Bid class which is actually making the bidlist. I.e:

void theBidlist ( vector<Bid> &list )
{   
 int numbids = 20;  
 for ( int i = 0;i < numbids;i++ )   
   {      
   list.push_back ( getBids() );   
   }
}

i keep getting :

-on line 7(i.e vector<Bid> BidList (20);) : error 7 expected `;' before '(' token
-and in simulator:20 `BidList' undeclared (first use this function)

i keep getting :

-on line 7(i.e vector<Bid> BidList (20);) : error 7 expected `;' before '(' token
-and in simulator:20 `BidList' undeclared (first use this function)

The compiler does not have a clue about what a 'Bid' is at line 7.
You might swap the order of the classes, like so ..

/////////////////////////
// First, class Bid
/////////////////////////
class Bid
{
private:

   int bidID, quantity, price, traderid;
   char tradertype;
public:
   static int BidID;
   Bid ( int tid, char ttype, int p , int q ) : traderid ( tid ), tradertype ( ttype ), quantity ( p ), price ( q )
   {
      bidID = Bid::BidID++;
   }

... etc

/////////////////////////
// Then, class Simulator
/////////////////////////

class Simulator 
{

// Now able to use Bid here ...
vector<Bid> BidList (20);

public:
  
   void run();
   void match();
};

void Simulator::run() 
{
... etc

well you need to move your bid class above your simulator class to fix the second error because you are trying to use it before you have defined it. for the first error try using

list.push_back ( this->getBids() );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.