samedi 17 novembre 2018

callbacks fails when std::bind and std::function used

There are two class server and client. client calls method datas() on server. The server responds to the caller with the datalist using datacallback().

I see compile time error in function clientfun2 when serverobj.datas() is called. Please help to fix it.

#include <iostream>
#include <functional>
#include <memory>
#include <string>
#include <vector>

enum Status { SUCCESS, FAILED, UNKOWN };

typedef std::vector<std::string> datalist;

class server {
 public:
  typedef std::function<void(int, Status, const datalist&)> Callback;
  void datas(int request_id, Callback datacallback) {
    datalist data; //here data is inserted and set to caller
    std::cout << "Invoked datas method\n";
    datacallback(123, SUCCESS, data); // sending back to caller
  }
};

class client {
 public:
  void clientfun1(int req_id, Status status, datalist& datas) {
    std::cout << "Invoked clientfun1\n";
  }

  void clientfun2(server serverobj) {
    serverobj.datas(123,
                    std::bind(&client::clientfun1, this, std::placeholders::_1,
                              std::placeholders::_2, std::placeholders::_3));  /*Here the error comming pls help to fix */[![Error screen shot][1]][1]
  }
};

int main() {
  server serverobj;
  client clientobj;
  clientobj.clientfun2(serverobj);
}

Aucun commentaire:

Enregistrer un commentaire