我正在尝试这段代码
demo.hpp
#ifndef DEMO_HPP
#define DEMO_HPP
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <vector>
using namespace std;
typedef boost::function<void(vector<int>)>func;
typedef void (_stdcall *Callback);
class funcPointer
{
public:
void add_call(int, func);
void call_back(int, Callback);
void push_elements();
vector<int> vec;
};
#endif
demo.cpp
#include <iostream>
#include "demo.hpp"
void funcPointer::add_call(int number, func f)
{
cout<<"number: "<<number << endl;
f(vec);
}
void funcPointer::push_elements()
{
vec.push_back(11);
vec.push_back(12);
vec.push_back(13);
}
void funcPointer::call_back(int x, Callback call)
{
cout << "x: " << x <<endl;
}
的main.cpp
#include <iostream>
#include "demo.hpp"
void display(vector<int> v)
{
vector<int> ::iterator it;
for(it = v.begin(); it != v.end(); it++)
{
cout<< *it <<endl;
}
}
void Inside_callback()
{
cout << "Hello World" << endl;
}
int main()
{
funcPointer *fun = new funcPointer;
fun->push_elements();
fun->add_call(24, boost::bind(display, _1));
fun->call_back(10, &Inside_callback);
return 0;
}
编译时我收到以下错误:
e:\vs_c++\boost_func_ptr\boost_func_ptr\demo.hpp(12): error C2165: 'left-side modifier' : cannot modify pointers to data
我无法理解这个错误是什么,以及如何摆脱它。 有人可以帮我摆脱这个错误吗?
答案 0 :(得分:0)
您必须按以下方式定义回调类型:
typedef void (_stdcall *Callback)();
您还应该按照以下方式修改Inside_callback的声明,以便编译代码:
void _stdcall Inside_callback()