Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8809321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:50:44+00:00 2026-06-14T02:50:44+00:00

Info: Parent Class: Vehicle Child Class : Car & Lorry I got a vector

  • 0
Info:
Parent Class: Vehicle
Child Class : Car & Lorry

I got a vector and i trying to sort it by ascending area

sort(myVector.begin(),myVector.end(),compareByArea);

so at the Vehicle.h

I did

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

and at Vehicle.cpp i did this

bool VehicleTwoD::compareByArea(const VehicleTwoD &a,const VehicleTwoD &b)
{
return a.getArea() < b.getArea();
}

Error was VehicleTwoD has no member named getArea.

It is located in my child Car & Lorry and double area is also declare in the child .

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

Is there a better way to do it .

Thanks for all help!

Additional Info:

I have this getArea() function at Car & Lorry which is just a simple

double Car::getArea()
{ return area;}

double Lorry::getArea()
{ return area;}

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

sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
sort(sortVector.begin(),sortVector.end(),compareArea);

//but the sort doesnt work.

I don’t know where to create compareArea which can getArea() of object 1 against object 2 and return bool result;

I try to create it in VehicleTwoD using this

bool VehicleTwoD::compareByArea(const VehicleTwoD &a,const VehicleTwoD &b)
{
return a.getArea() < b.getArea();
}

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

Thanks to the help:

I tried the following

template<typename T> bool compareByArea(const T &a, const T &b) {
    return a.getArea() < b.getArea();
}

and i declare it at the .h file too,

public:
template<typename T>
bool compareByArea(const T,const T);
virtual double getArea();

then at my main.cpp i tried

sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
sort(sortVector.begin(),sortVector.end(),sortVector[0].compareArea);

It give me an error

error request for member compareByArea in sortVector.std::vector<_TP, _ALLOC ……. which is of non class type ‘VehicleTwoD*’

If i do this

  sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
  sort(sortVector.begin(),sortVector.end(),sortVector[0].compareArea);

I will get error compareByArea was not declared in this scope.

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

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

What should i do.. confused & stuck.
Thanks!

Latest Update Again:

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.

The problem is getArea is a function of Car & Lorry (child class) and i create this compareByArea at main.cpp

sortVector is a vector copy of vehicletwod

The way i set value into my vehicletwod is this way..

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

How do i achieve my sorting by area ascending.

I created this function at main.cpp

template<typename T> bool compareByArea(const T &a, const T &b) {
    return a.getArea() < b.getArea();
}

then i did this

sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
sort(sortVector.begin(),sortVector.end(),compareByArea);

Compile Error:
Compile error:

no matching function for call to 'sort(std::vector<VehicleTwoD*>::iterator, std::vector<VehicleTwoD*>::iterator, <unresolved overloaded function type>)'

note: template<class _RAIter> void std::sort (_RAIter, _RAIter)
note: template<class _RAIter, class _Compare> void std::sort(_RAiter, _RAIter, _Compare)

Thanks

I got a new compile error

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.

Regarding my getArea is this

//in parent is this way

double VehicleTwoD::getArea()
{
double area;
area=0.00;
return area;
}

Car – Child is

 double Car::getArea()
    {
    return area;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:50:46+00:00Added an answer on June 14, 2026 at 2:50 am

    Make a template function:

    template<typename T> bool compareByArea(const T *a, const T *b) {
        return a->getArea() < b->getArea();
    }
    

    then you can pass it to sort:

    sort(sortVector.begin(), sortVector.end(), compareByArea<VehicleTwoD>);
    

    Update:

    Vehicle.h:

    class VehicleTwoD {
    ...
    public:
        virtual double getArea() const;
    };
    
    template<typename T> bool compareByArea(const T *a, const T *b) {
        return a->getArea() < b->getArea();
    }
    

    main.cpp:

    #include "Vehicle.h"
    #include <vector>
    ...
    std::vector<VehicleTwoD*> sortVector;
    // fill sortVector
    sort(sortVector.begin(), sortVector.end(), compareByArea<VehicleTwoD>);
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find the class .post-info inside the parent of a clicked element,
I have a Parent / Child class hierarchy where the Parent abstractly declares a
Info: I'm currently trying to learn template metaprogramming (by following this book ). One
I wonder why static objects call parent method and dynamic objects child method in
I have two objects: public class ParentObject { // some basic bean info }
class File { String name File parent; static belongsTo =[parent:File ] static hasMany =
(edit) more info. First notice new virtual. This class inherits a base class which
I have the following class define with some info that I would like to
I have class with member functions: typedef std::function<bool (const std::string &)> InsertFunction; bool insertSourceFile(
I have a problem where I have this three grandfather parent and child and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.