Я использую for_each и mem_fun_ref в качестве примера, но есть некоторые ошибки при компиляции, в чем проблема
#include<iostream>
#include<algorithm>
#include<set>
#include<iterator>
using namespace std;
class Tst
{
public:
Tst(int a, string b):n(a),s(b)
{}
bool operator<(const Tst& t)const
{
return this->n < t.n;
}
int GetN()const
{
return n;
}
string GetS()const
{
return s;
}
void SetN(int a)
{
n = a;
}
void SetName(string name)
{
s = name;
}
void Print(void)
{
cout <<"n is:" << n <<"\ts is:" << s << endl;
}
private:
int n;
string s;
};
int main(void)
{
typedef set<Tst> TstSet;
TstSet tst;
tst.insert(Tst(10, "abc"));
tst.insert(Tst(1, "def"));
for_each(tst.begin(), tst.end(), mem_fun_ref(&Tst::Print));
return true;
}
: 4200: : : 对 ‘(std :: mem_fun_ref_t) (const Tst&)»的 调用 没有 匹配, 是 什么 原因
std::set
содержащиеся объекты const
так что звонить можно только const
функции на них. Ваш Print
функция должна быть отмечена const
,
Функция должна быть const
, так как std::set
работает только с const
объекты
void Print(void)const
{
}