error: ‘class car’ has no member named ‘length’
return a.length() < b.length();
My code is giving error on length function what shall i do?
#include
#include
#include
#include
#include <bits/stdc++.h>
using namespace std;
class car{
public:
string car_name;
int x,y;
car(){
}
car(string n , int x , int y){
car_name = n;
this->x = x;
this->y = y;
}
int dist(){
// returns square of distance from origin
return (x*x + y*y);
}
};
bool compare(car a , car b){
int da = a.dist();
int db = b.dist();
int dla = a.length(); // its giving error here
int dlb = b.length();
if(da == db){
return dla < dlb;
}
da<db;
}
int main()
{
int n;
std::cin >> n;
vector<car> v;
for (int i = 0; i < n; i++) {
int x,y;
string name;
cin>>name>>x>>y;
car temp(name,x,y);
v.push_back(temp);
}
sort(v.begin() , v.end() , compare);
for(auto c : v){
cout<<"Car "<<c.car_name<<"Distance"<<c.dist()<<"Location"<<c.x<<","<<c.y<<endl;
}
return 0;
}
please save your code on coding blocks ide and send link so that i can try it.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.