passing a vector from one class to another.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 3395 Views - Last Post: 15 May 2010 - 10:33 AM Rate Topic: -----

#1 Guest_james_wills*


Reputation:

passing a vector from one class to another.

Posted 09 May 2010 - 04:19 PM

I am trying to pass a vector of objects from one class to another.

//Class of Origin of the vector.
class Trader {   
private:   
        int nextBidId; 
 
public:   
        Trader(); 
        ~Trader(){};  
        Bid getNextBid();  
        Bid getNextBid(int trdId, int qty, int price, char type);  
        void loadRange( vector <Bid> & bids ) {} ;
        void loadRange(BidList &, int trdId, int qty, int price, char type, int size); 
 
}; 
//To be received by the Simulator
 class Simulator 
{ 
    vector <Bid> list;
    Trader trader; 
    Auctioneer auctioneer; 
 
public:
    void run(); 
}; 

// Passing the vector into a function in simulator
Simulator::accept_bids(bid_vector::const_iterator begin, bid_vector::const_iterator end){
     vector<Bid>::iterator itr;
}
//Its journey should end with the Auctioneer. who displays the data
class Auctioneer 
{ 
  
public: 
    vector <Bid>v2;// created a new vector to hold the objects
    void accept_bids(vector<Bid> & bids);
    void displayBids(){return bids}
};


Is This A Good Question/Topic? 0

Replies To: passing a vector from one class to another.

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: passing a vector from one class to another.

Posted 09 May 2010 - 04:46 PM

What's the problem with the code?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_james_wills*


Reputation:

Re: passing a vector from one class to another.

Posted 09 May 2010 - 07:21 PM

The problem is that the vector of bids is not being received by the simulator.Prabably am not using a proper method of sending the vector

Error : `str' undeclared (first use this function) - Is it not part of the #string lib. 3include<cstring>

Trader class\
class Trader {   

private:   
        int nextBidId;
        string toString(const Bid &bid); 
//function for making the stream

string Trader::toString(const Bid &bid) {
	stringstream stream;
	stream << "\tBid\t(" <<  setw(3) << bid.bidId << "\t " << setw(3) << bid.trdId  << "\t ";  
    stream << setw(3) <<  bid.type <<"\t " << setw(3) << bid.qty <<"\t "  << setw(3) << bid.price <<")\t\n " ; 
	return stream.str();
}

//-------------------------------------------------------------------------------------------------
class Simulator 
{ 
    vector <Bid> bids;
    Trader trader; 
    Auctioneer auctioneer; 
 
public: 
    void run(); 
}; 
 
void Simulator::run() { 
    //trader.loadRange(bidlist, NUMBIDS);  
    auctioneer.receiveBids(trader.toString(const Bid &bid)); 
    auctioneer.displayBids();
} 

//-------------------------------------
class Auctioneer  
{ 
  string ss;
  vector <Bid> bidList; 
public:
    //void loadRange(bidlist);  
    string receiveBids(string ss){ss = bids;}
    string displayBids(string ss){return str;} 
};


Was This Post Helpful? 0

#4 Guest_james_wills*


Reputation:

Re: passing a vector from one class to another.

Posted 13 May 2010 - 11:23 AM

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


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



Was This Post Helpful? 0

#5 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: passing a vector from one class to another.

Posted 13 May 2010 - 01:16 PM

Could you point out the line/s of code producing the error or post your full code so we can compile it and look.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_james_wills*


Reputation:

Re: passing a vector from one class to another.

Posted 13 May 2010 - 01:52 PM

The error comes up in the Simulator class.

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

// 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;  
//} 
// 
//void show(const char *msg, const BidList &list) {  
//        cout << msg << endl; 
//        show(list); 
//}  
// search now checks for failure 
//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); }  
//-----------------------------------------------------------------

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

};
                                                                 
//void show(const Auctioneer &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 Auctioneer::displayBids() {
//              cout << "\tBid\t(" <<  setw(3) << lst.bidId << "\t " << setw(3) << lst.trdId  << "\t " 
//     << setw(3) <<  lst.type <<"\t " << setw(3) << lst.qty <<"\t "  << setw(3) << lst.price <<")\t\n "  ;    
//    }  
                       
//---------------------------------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;  
}


Was This Post Helpful? 0

#7 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: passing a vector from one class to another.

Posted 13 May 2010 - 06:18 PM

Your function call on 199 doesn't match any of the function definitions. According to your code, you need at least two parameters to call loadRange().

On line 200, there is no function recieveBids() declared anywhere in your code.

On line 201, you forgot the parethesis on the displayBids() function.

Line 210, You have no constructor that accepts no parameters. You can either create one or you one of the constructors that you have defined.

Line 212, there is no function called loadRange() in the Auctioneer class. Also you don't have a bidlist declared anywhere. Declare one before using it.

Line 213, and 215, you have no show() function declared anywhere in the code

Line 222 and 223, you don't have a search function declared in your code.


Well those are all the errors in your code. I suggest you declare the respective functions and variables and try to compile again.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_james_wills*


Reputation:

Re: passing a vector from one class to another.

Posted 13 May 2010 - 09:09 PM

woo! so many error.Well fixed them and cut short the code.the receiver Auctioneer

Error: in lookup_member, at cp/search.c:1288

class Auctioneer {

public:
      Auctioneer(){cout<<"Am waiting for the bids\n";}

      Trader trader;
      
      void accept_bids(std::vector<Bid> BidList);
      typedef vector<Auctioneer> vec;
      typedef vector<Auctioneer> buyerList;
      typedef vector<Auctioneer> sellerList;

};

void Auctioneer::accept_bids(std::vector<Bid>BidList)
     {
        Auctioneer bids;
        bids.vector<Bid>BidList = vec;
        vec.push_back(bids); 
     }


Was This Post Helpful? 0

#9 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: passing a vector from one class to another.

Posted 14 May 2010 - 12:12 PM

Post your full code again.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_james_wills*


Reputation:

Re: passing a vector from one class to another.

Posted 14 May 2010 - 05:28 PM

the program executes but comes back empty.I really dont know where the bids generated disapear from.


#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;
    //copy (BidList.begin(),BidList.end(),list);    
}

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


Was This Post Helpful? 0

#11 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: passing a vector from one class to another.

Posted 14 May 2010 - 05:49 PM

Look at your accept_bids function. If you trace it all the way to the Bidlist class you will see that nothing is even happening. You are calling the default constructor but no data is stored anywhere.
Was This Post Helpful? 0
  • +
  • -

#12 Guest_james_wills*


Reputation:

Re: passing a vector from one class to another.

Posted 14 May 2010 - 11:56 PM

I tried to use this, but it doesnt copy.the other alternative I tried was creating another container vector in the Auctioneer class but didnt work too, since the data type was different.

Still got an error : no match for 'operator<<' in 'std::cout << iter'

The compiler is still complaining of the << . no match to the operator.

[code]
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 << "\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 "  ;  
    }
     cout << *iter << endl <<"\n";    
}


[/code]
Was This Post Helpful? 0

#13 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: passing a vector from one class to another.

Posted 15 May 2010 - 12:00 AM

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


Since BidList is a STL vector, containing Bid class objects, that means "*iter" is an object of "Bid" class. You can't output a class like that. You need to overload the "<<" operator.
Was This Post Helpful? 0
  • +
  • -

#14 Guest_james_wills*


Reputation:

Re: passing a vector from one class to another.

Posted 15 May 2010 - 12:16 AM

I did try to overload it too,still got an error on the cout.

probaly I didnt do it the right way
ostream& operator<<(ostream& cout )
             {
               cout << bid.bidId  << bid.trdId  <<  bid.qty<< bid.qty << bid.price << bid.type;
               return cout;
            }


Was This Post Helpful? 0

#15 Guest_james_wills*


Reputation:

Re: passing a vector from one class to another.

Posted 15 May 2010 - 02:29 AM

I have tried to clean up the code, especially the calling function, but still have no out put.Is there anything that is wrong with this function?
void Auctioneer::accept_bids(const BidList& bidlist, int)
{ 
 
    for (BidList::const_iterator iter = bidlist.begin(); 
                                 iter != bidlist.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; 
    } 
} 


Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2