std :: bindおよびstd :: functionを使用すると、コールバックが失敗します

josp

2つのクラスのサーバーとクライアントがあります。クライアントはサーバー上のメソッドdatas()呼び出します。サーバーは、datacallback()を使用してデータリストで呼び出し元に応答します

serverobj.datas()が呼び出されると、関数clientfun2コンパイル時エラーが表示されますそれを修正するのを手伝ってください。

#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 */
  }
};

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

****テキスト形式のエラー****

bind.cc: In member function ‘void client::clientfun2(server)’:
bind.cc:30:76: error: no matching function for call to ‘server::datas(int, std::_Bind_helper<false, void (client::*)(int, Status, std::vector<std::__cxx11::basic_string<char> >&), client*, const std::_Placeholder<1>&, const std::_Placeholder<2>&, const std::_Placeholder<3>&>::type)’
                               std::placeholders::_2, std::placeholders::_3));
                                                                            ^
bind.cc:14:8: note: candidate: void server::datas(int, server::Callback)
   void datas(int request_id, Callback datacallback) {
        ^
bind.cc:14:8: note:   no known conversion for argument 2 from ‘std::_Bind_helper<false, void (client::*)(int, Status, std::vector<std::__cxx11::basic_string<char> >&), client*, const std::_Placeholder<1>&, const std::_Placeholder<2>&, const std::_Placeholder<3>&>::type {aka std::_Bind<std::_Mem_fn<void (client::*)(int, Status, std::vector<std::__cxx11::basic_string<char> >&)>(client*, std::_Placeholder<1>, std::_Placeholder<2>, std::_Placeholder<3>)>}’ to ‘server::Callback {aka std::function<void(int, Status, const std::vector<std::__cxx11::basic_string<char> >&)>}’

エラーのスクリーンショット

一部のプログラマーの男

我々が取る場合はstd::function、右隣同士に実際の機能は、それが違いを見るのは簡単だ(良いことを示すために、再フォーマット):

//                                                    vvvvv
typedef std::function<void(int       , Status       , const datalist&)> Callback;
void clientfun1(           int req_id, Status status,       datalist& datas) {
//                                                    ^^^^^

これで、std::functionオブジェクトの最後の引数は定数 への参照datalistであるのに対し、clientfun1関数で定数ではないことがわかります

これにより、2つの関数タイプが異なり、エラーが発生します。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

std :: bindおよびstd :: function with polymorphism、base class

分類Dev

std :: bindをstd :: functionおよびstd :: mapと一緒に使用する方法

分類Dev

std :: functionおよびstd :: bindの戻り値

分類Dev

std :: functionおよびstd :: bindでC ++テンプレートを使用する

分類Dev

std :: bind2ndおよびstd :: bindと2次元配列および構造体の配列

分類Dev

constクラスオブジェクトをstd :: functionおよびstd :: bindに渡すとコンパイルエラーが発生します

分類Dev

C ++ 11:継承でstd :: mem_fnおよびstd :: bindを使用する方法

分類Dev

std :: bindおよびstd :: functionを使用して、クラスメンバー関数を別のクラスメンバールーチンのコールバックとして使用する

分類Dev

std :: bindおよび関数テンプレート

分類Dev

std :: functionおよびstd :: bind:それらは何であり、いつ使用する必要がありますか?

分類Dev

std :: bindがstd :: filesystem :: pathおよびstd :: ostreamで機能しないのはなぜですか?

分類Dev

std :: bindおよびstd :: placeholdersを使用する可変個引数テンプレートファクトリ

分類Dev

std :: bindおよびstd :: weak_ptr

分類Dev

clang-6および-std = c ++ 17-bind(2)を呼び出せません

分類Dev

std :: bindおよびstd :: threadのセマンティクス/動作を移動します

分類Dev

std :: bindとメンバー関数およびこれはc ++で

分類Dev

メンバー関数を使用すると、std :: result_ofを使用したコンパイルが失敗する

分類Dev

std :: stoi、std :: setbase、およびstd :: out_of_rangeエラー

分類Dev

ソートされたベクトルのstd :: find_ifおよびstd :: bind2ndの置換

分類Dev

パラメータを使用したvoidメソッドをstd :: functionおよびstd :: bind C ++を使用した別のメソッドに渡す

分類Dev

std :: sscanfおよびstd :: stringを使用するC ++

分類Dev

std :: promiseおよびstd :: futureを使用すると、アクセス違反が発生します

分類Dev

std :: bindおよび右辺値の参照

分類Dev

std :: bindおよびstack-use-after-scope

分類Dev

clang、std :: functionおよび-fno-rtti

分類Dev

std :: bindおよび基本クラスメソッドへの非仮想呼び出し

分類Dev

std :: regexおよびデュアルABI

分類Dev

可変個引数テンプレートを使用したstd :: ref()およびstd :: bind()について少しぼんやりしています

分類Dev

std :: bindおよび重複プレースホルダーを使用した未定義の結果

Related 関連記事

  1. 1

    std :: bindおよびstd :: function with polymorphism、base class

  2. 2

    std :: bindをstd :: functionおよびstd :: mapと一緒に使用する方法

  3. 3

    std :: functionおよびstd :: bindの戻り値

  4. 4

    std :: functionおよびstd :: bindでC ++テンプレートを使用する

  5. 5

    std :: bind2ndおよびstd :: bindと2次元配列および構造体の配列

  6. 6

    constクラスオブジェクトをstd :: functionおよびstd :: bindに渡すとコンパイルエラーが発生します

  7. 7

    C ++ 11:継承でstd :: mem_fnおよびstd :: bindを使用する方法

  8. 8

    std :: bindおよびstd :: functionを使用して、クラスメンバー関数を別のクラスメンバールーチンのコールバックとして使用する

  9. 9

    std :: bindおよび関数テンプレート

  10. 10

    std :: functionおよびstd :: bind:それらは何であり、いつ使用する必要がありますか?

  11. 11

    std :: bindがstd :: filesystem :: pathおよびstd :: ostreamで機能しないのはなぜですか?

  12. 12

    std :: bindおよびstd :: placeholdersを使用する可変個引数テンプレートファクトリ

  13. 13

    std :: bindおよびstd :: weak_ptr

  14. 14

    clang-6および-std = c ++ 17-bind(2)を呼び出せません

  15. 15

    std :: bindおよびstd :: threadのセマンティクス/動作を移動します

  16. 16

    std :: bindとメンバー関数およびこれはc ++で

  17. 17

    メンバー関数を使用すると、std :: result_ofを使用したコンパイルが失敗する

  18. 18

    std :: stoi、std :: setbase、およびstd :: out_of_rangeエラー

  19. 19

    ソートされたベクトルのstd :: find_ifおよびstd :: bind2ndの置換

  20. 20

    パラメータを使用したvoidメソッドをstd :: functionおよびstd :: bind C ++を使用した別のメソッドに渡す

  21. 21

    std :: sscanfおよびstd :: stringを使用するC ++

  22. 22

    std :: promiseおよびstd :: futureを使用すると、アクセス違反が発生します

  23. 23

    std :: bindおよび右辺値の参照

  24. 24

    std :: bindおよびstack-use-after-scope

  25. 25

    clang、std :: functionおよび-fno-rtti

  26. 26

    std :: bindおよび基本クラスメソッドへの非仮想呼び出し

  27. 27

    std :: regexおよびデュアルABI

  28. 28

    可変個引数テンプレートを使用したstd :: ref()およびstd :: bind()について少しぼんやりしています

  29. 29

    std :: bindおよび重複プレースホルダーを使用した未定義の結果

ホットタグ

アーカイブ