2016-02-28 87 views
0

我试图这段代码错误C2165: '左侧改性剂':不能修改数据指针

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; 
} 

主.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

您有以下方式来定义回调的类型:

typedef void (_stdcall *Callback)(); 

你也应该修改如下方式进行代码编译Inside_callback声明:

void _stdcall Inside_callback()