for_each and mem_fun_ref trouble
for_each and mem_fun_ref trouble
我以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
{
}
相关文章:
- 对RValue对象调用的LValue ref限定成员函数
- 将Ref对象作为类成员
- 为什么我的 std::ref 无法按预期工作?
- 如何将 Eigen::Ref 与 pybind11 一起使用?
- 如何@ref同一方法的不同变体?
- Boost.Asio/OpenSSL HTTPS GET certificate trouble
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- std::bind 是否实现了 std::ref 和 std::cref 来消除函数调用的歧义?
- 为什么要用 ref-qualifier 和
- 将 ArrayXd 传递给 const VectorXd& 和 const Ref<const VectorXd>&
- 返回 Eigen::Ref 合法吗?
- 将对象传递给函数而不将其包装到 std::ref 中,而参数被指定为 const 引用
- 使用 const char* 初始化 const ref 字符串成员时幕后会发生什么
- 为什么添加析构函数(甚至是空的)会破坏我的结构,该结构使用 ref 转发和折叠来保存 ref 或值的副本?
- 在与Odint组合的类中使用特征矩阵Ref
- const-ref传递的模板化参数是否经过优化,以便在足够小时按值传递
- 带有自定义deleter的std::unique_ptr对象的大小(一个由ref捕获的lambda)
- 正在通过const-ref未定义的行为捕获新构造的对象
- C++将包含的库 (ref DLL) 从根目录更改为另一个位置
- 无法修改其他线程中 ref 传递的值