Пытаюсь скопировать обьекты list в set.Выдаёт ошибку
#include <iostream>
#include <set>
#include <list>
#include <time.h>
#include <algorithm>
using namespace std;
class point
{
private:
int x;
int y;
public:
point()
{
x = 0;
y = 0;
}
point(int a, int b)
{
x = a;
y = b;
}
void operator =(point& a)
{
this->x = a.x;
this->y = a.y;
}
const bool operator <(point& a) const
{
if ((this->x +this->y)< (a.x + a.y))
{
return true;
}
else
{
return false;
}
}
bool operator >(point& a)const
{
if ((this->x + this->y) > (a.x + a.y))
{
return true;
}
else
{
return false;
}
}
bool operator ==(point& a)const
{
if ((this->x == a.x)&&(this->y == a.y))
{
return true;
}
else
{
return false;
}
}
void show()
{
cout << '(' << x << "," << y << ") ";
}
int GetX() { return x; };
int GetY() { return y; };
};
//template<typename T1>
auto copy_if(list<point> &a,set<point> &b)
{
int current_x;
int current_y;
cout << "x = "; cin >> current_x;
cout << "y = "; cin >> current_y;
point current(current_x, current_y);
for (auto &x : a)
{
if (x < current)
{
b.insert(x);
}
}
return b.begin();
}
int main()
{
srand(time(NULL));
int N;
int current_x;
int current_y;
cout << "N = "; cin >> N;
list<point> l;
for (int i = 0; i < N; i++)
{
l.push_back(point(rand() % 100, rand() % 100));
}
l.sort();
for (auto& i : l)
{
i.show();
}
cout << endl;
cout << "x = "; cin >> current_x;
cout << "y = "; cin >> current_y;
point current(current_x, current_y);
auto a = find_if(l.begin(), l.end(), [&](point& a){return (a == current);});
if (a == l.end())
{
}
else
{
(*a).show();
}
set<point> s;
/*cout << "x = "; cin >> current_x;
cout << "y = "; cin >> current_y;
point current1(current_x, current_y);
remove_copy_if(l.begin(), l.end(), s.begin(),[&](point& c) {return (c > current1);});*/
copy_if(l, s);
for (auto& i : s)
{
}
}
Помогите пожалуйста, не понимаю, он выдаёт ошибку что не найдет оператор"<"хотя я перегрузил все операторы
Ответы (1 шт):
Автор решения: Mikhailo
→ Ссылка
Не с той стороны const поставили: вместо
const bool operator <(point& a) const
нужно
bool operator <(const point& a) const
Выработайте привычку везде, где объект не меняется, ставить const. Можете даже так - ставить const везде, а потом убирать там, где он мешает - но только разобравшись, в чем дело.