I am trying to sort a vector that has an object of a derived class, where the base class have protected members
In my header I have the base class:
class Item{
protected:
int ID;
string name;
int cost;
int sell;
int profit;
float profitperh;
int time; // in seconds!
vector<string> usedFor;
public:
//getters
//setters
//functions
virtual bool DescProfit (const Item& i1, const Item& i2) const;
bool operator < (const Item& i1) const;
}
From Base class I derive a couple of objects, the one I am currently working with to see if it works fine is named Seed and looks like this:
class Seed : public Item{
private:
public:
//Constructors
virtual bool operator < (const Item& i1) const{
return (profit < i1.Item::profit); // Also tried this line with i1.Item::profit and i1.Item::getProfit() and i1.Item::profit
}
// virtual bool DescProfit (const Item& i1, const Item& i2) const{
// return i1.profit > i2.profit; // Also tried this line with i1.Item::profit and i1.Item::getProfit() and i1.Item::profit
// } //end DescProfit
When I try to build this it complains on the return line that 'profit' is a protected member of 'Item' The operator < and DescProfit are to be used with sort(), to sort the vector:
vector<Item*> v1;
sort(v1.begin(), v1.end(), DescProfit);
I've tried to place the bool function both inside and outside of class with the same error mentioned above. Does anyone have any ideas to what the error might be? I got the "template" for the above code from this post: Sorting a vector of custom objects What do I need to do to be able to access the protected variable in the base class?
Thank you in advance