Sorting a Vector of Class Object
Oct 1, 2012 at 2:18am UTC
I am trying to do a selection sort with a vector passed as a reference and compare different vector object elements. Is this possible?
The error I'm getting is:
1>c:\users\rodrigo\documents\visual studio 2010\projects\salesorder\salesorder\source1.cpp(93): error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'Order'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(1502) : see declaration of 'std::operator <'
Like I said, I'm novice when it comes to classes and what is allowable so please be patient with me.
My code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
class Order
{
int bidPrice;
int lots;
int acctNum;
public :
Order (int bidPrice, int lots, int acctNum);
Order () {};
void assignOrder (int , int , int );
void sortOrders (vector<Order>& orders, int );
void printOrder ();
//void assignPrice (int newPrice);
//void assignLots (int newLots);
//void assignAcct (int newAcct);
bool executable(int offerPrice);
void execute(void );
};
//Order Orders;
int main ()
{
vector <Order> orders;
Order tmpOrder;
int bPrice;
int bLots;
int bAcct;
// int offer;
string logOff;
cout << "Welcome to the Financial Instrument Order System\n" ;
cout << "To exit the program at any time, please type Quit\n" ;
cout << "Please Enter Your Order\n" ;
cout << endl;
cout << endl;
cout << "Price Lots Account Number\n" ;
cout << "_____ ____ ______________\n" ;
while (logOff != "Quit" )
{
cin >> left >> setw(5) >> bPrice;
cin >> left >> setw(5) >> bLots;
cin >> left >> setw(5) >> bAcct;
tmpOrder.assignOrder(bPrice, bLots, bAcct);
for (int i=0; i <= orders.size(); i++)
{
orders.push_back(tmpOrder);
cout << "\n" ;
tmpOrder.sortOrders(orders, orders.size());
tmpOrder.printOrder();
cout << "\n" ;
break ;
}
}
return 0;
}
void Order::assignOrder(int bPrice, int bLots, int bAcct)
{
bidPrice = bPrice;
lots = bLots;
acctNum = bAcct;
}
void sortOrders(vector<Order>& orders, int length)
{
int index;
int smallestIndex;
int location;
Order temp;
for (index = 0; index < length - 1; index++)
{
//Step a
smallestIndex = index;
for (location = index + 1; location < length; location++)
if (orders[location] < orders[smallestIndex])
smallestIndex = location;
//Step b
temp = orders[smallestIndex];
orders[smallestIndex] = orders[index];
orders[index] = temp;
}
}
Last edited on Oct 1, 2012 at 2:30am UTC
Oct 1, 2012 at 3:36am UTC
Order doesn't overload operator<(), so you can't compare two Orders.
Oct 1, 2012 at 6:30am UTC
Add the following to your Order class. This will allow line 93 to work.
1 2 3 4
bool operator < (Order& o)
{
return bidPrice < o.bidPrice;
}
Last edited on Oct 1, 2012 at 6:31am UTC
Topic archived. No new replies allowed.