错误 C2165:'left-side modifier':无法修改指向数据的指针

error C2165: 'left-side modifier' : cannot modify pointers to data

本文关键字:数据 修改 指针 modifier C2165 left-side 错误      更新时间:2023-10-16

我在尝试这段代码

演示.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

演示.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_ptrboost_func_ptrdemo.hpp(12): error C2165: 'left-side modifier' : cannot modify pointers to data

我无法理解这个错误是什么,以及如何摆脱它。有人可以帮助我摆脱这个错误吗?

您必须通过以下方式定义回调的类型:

typedef void (_stdcall *Callback)();

您还应该按以下方式修改Inside_callback声明,以便编译代码:

void _stdcall Inside_callback()