C++ vector sorting with my own Function
<pre><code>Info:
Parent Class: Vehicle
Child Class : Car &amp; Lorry
</code></pre>

<p>I got a vector and i trying to sort it by ascending area</p>

<p>sort(myVector.begin(),myVector.end(),compareByArea);</p>

<p>so at the Vehicle.h</p>

<p>I did </p>

<pre><code>class VehicleTwoD
{
private:
//some variable
public:
bool compareByArea(const VehicleTwoD,const VehicleTwoD);
}
</code></pre>

<p>and at Vehicle.cpp i did this</p>

<pre><code>bool VehicleTwoD::compareByArea(const VehicleTwoD &amp;a,const VehicleTwoD &amp;b)
{
return a.getArea() &lt; b.getArea();
}
</code></pre>

<p>Error was VehicleTwoD has no member named getArea.</p>

<p>It is located in my child Car &amp; Lorry and double area is also declare in the child .</p>

<p>But the issue is if i want to sort by this way, could i achieve it by using virtual and then I create the same method at child but overload it and get my area sort by that way </p>

<p>Is there a better way to do it .</p>

<p><strong>Thanks for all help!</strong></p>

<pre><code>Additional Info:
</code></pre>

<p>I have this getArea() function at Car &amp; Lorry which is just a simple </p>

<pre><code>double Car::getArea()
{ return area;}

double Lorry::getArea()
{ return area;}
</code></pre>

<p>But now as i got a list of object in my vector, each of them is a child of different class. but all got getArea function, i want to sort this vector which is </p>

<pre><code>sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
sort(sortVector.begin(),sortVector.end(),compareArea);
</code></pre>

<p>//but the sort doesnt work.</p>

<p>I don't know where to create compareArea which can getArea() of object 1 against object 2 and return bool result;</p>

<p>I try to create it in VehicleTwoD using this</p>

<pre><code>bool VehicleTwoD::compareByArea(const VehicleTwoD &amp;a,const VehicleTwoD &amp;b)
{
return a.getArea() &lt; b.getArea();
}
</code></pre>

<p>But the issue is VehicleTwoD don't have the function getArea, they are located at child. how should i get this fix . Thanks..</p>

<p><strong>Thanks to the help:</strong></p>

<p>I tried the following</p>

<pre><code>template&lt;typename T&gt; bool compareByArea(const T &amp;a, const T &amp;b) {
    return a.getArea() &lt; b.getArea();
}
</code></pre>

<p>and i declare it at the .h file too,</p>

<pre><code>public:
template&lt;typename T&gt;
bool compareByArea(const T,const T);
virtual double getArea();
</code></pre>

<p>then at my main.cpp i tried</p>

<pre><code>sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
sort(sortVector.begin(),sortVector.end(),sortVector[0].compareArea);
</code></pre>

<p>It give me an error</p>

<p>error request for member compareByArea in sortVector.std::vector&lt;_TP, _ALLOC ....... which is of non class type 'VehicleTwoD*'</p>

<p>If i do this</p>

<pre><code>  sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
  sort(sortVector.begin(),sortVector.end(),sortVector[0].compareArea);
</code></pre>

<p>I will get error compareByArea was not declared in this scope.</p>

<p>What should i do? I declared compareByArea at VehicleTwoD which is included in main.cpp </p>

<p>VehicleTwoD is the parent class, and i also virtual double getArea() so when it call getArea it will use the child getArea instead, because the private variable - area is at child and the function getArea() return area is also at child</p>

<p><strong>What should i do.. confused &amp; stuck.
Thanks!</strong></p>

<p><strong>Latest Update Again:</strong></p>

<p>I am trying to sort the vector by compareByArea which the smaller area will be sort at the highest while the bigger 1 will be at bottom.</p>

<p>The problem is getArea is a function of Car &amp; Lorry (child class) and i create this compareByArea at main.cpp</p>

<p>sortVector is a vector copy of vehicletwod</p>

<p>The way i set value into my vehicletwod is this way..</p>

<pre><code>if(vehicleType=="Car")
{
vehicletwod[arrayCount] = new Car();
vehicletwod[arrayCount].setDimension();
//set area
vehicletwod[arrayCount].setArea();
cout &lt;&lt; "Done setting the data";
}
</code></pre>

<p>How do i achieve my sorting by area ascending.</p>

<p>I created this function at main.cpp</p>

<pre><code>template&lt;typename T&gt; bool compareByArea(const T &amp;a, const T &amp;b) {
    return a.getArea() &lt; b.getArea();
}
</code></pre>

<p>then i did this</p>

<pre><code>sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
sort(sortVector.begin(),sortVector.end(),compareByArea);
</code></pre>

<p>Compile Error:
Compile error:</p>

<pre><code>no matching function for call to 'sort(std::vector&lt;VehicleTwoD*&gt;::iterator, std::vector&lt;VehicleTwoD*&gt;::iterator, &lt;unresolved overloaded function type&gt;)'

note: template&lt;class _RAIter&gt; void std::sort (_RAIter, _RAIter)
note: template&lt;class _RAIter, class _Compare&gt; void std::sort(_RAiter, _RAIter, _Compare)
</code></pre>

<p><strong>Thanks</strong></p>

<p>I got a new compile error</p>

<pre><code>error: passing 'const VehicleTwoD' as 'this' argument of 'virtual double VehicleTwoD::getArea()' discards qualifers.
error: passing 'const VehicleTwoD' as 'this' argument of 'virtual double VehicleTwoD::getArea()' discards qualifers.
</code></pre>

<p>Regarding my getArea is this </p>

<p>//in parent is this way</p>

<pre><code>double VehicleTwoD::getArea()
{
double area;
area=0.00;
return area;
}
</code></pre>

<p>Car - Child is </p>

<pre><code> double Car::getArea()
    {
    return area;
    }
</code></pre>
