compiling the following code via
g++ -std=c++11 test.cpp
gives me the following error:
test.cpp: In lambda function:
test.cpp:17:128: error: passing ‘const CMyclass’ as ‘this’ argument of ‘void CMyclass::my_method(const state_type&, double)’ discards qualifiers [-fpermissive]
std::function<void(const state_type &,const double)> observer = [=](const state_type &x,const double t){my_class.my_method(x,t);};
^
I have searched other similar questions but I can't figure out what is wrong with this code.
#include <vector>
#include <functional>
typedef std::vector<int> state_type;
class CMyclass
{
public:
void my_method( const state_type &x , const double t )
{
}
};
int main()
{
CMyclass my_class;
std::function<void(const state_type &,const double)> observer =
[=](const state_type &x,const double t)
{
my_class.my_method(x,t);
};
}
edit:
I would not put const
after the method.