[英]Vector of pointer member functions c++
我有以下代碼:
typedef bool (A::*rule)(shared_ptr<Node >, shared_ptr<Node> ) ;
在初始化 A 類對象時,我運行以下代碼
v_ = vector<rule>();
v_.push_back(A::memberFunction);
我也嘗試過 v_.push_back(&A::memberFunction) ,因為這對我來說似乎更合乎邏輯,但在論壇上被建議不要這樣做。 我也收到一條錯誤消息
正如關於此事的許多主題所建議的那樣。 但是,我收到以下錯誤
non-const lvalue reference to type 'value_type' (aka 'bool (boost::shared_ptr<Node>, boost::shared_ptr<Node>)')
cannot bind to a temporary of type '<bound member function type>'
有誰知道我哪里出錯了?
我也嘗試過 v_.push_back(&A::memberFunction) ,因為這對我來說似乎更合乎邏輯,但在論壇上被建議不要這樣做。
不知道是什么論壇。 形成指向成員的指針有一個有效的語法,它是& classname::membername
。 所有元素都是強制性的。
如果問題仍然存在,請始終使用該版本並發布該版本的代碼和錯誤消息。
此代碼在 llvm/clang 4.2 下編譯(正如@balog-pal 所說, &
在classname::membername
membername 之前是必需的:
#include <memory>
#include <vector>
struct Node {};
struct A
{
bool rule(std::shared_ptr<Node>, std::shared_ptr<Node>)
{return true;}
};
typedef bool (A::*rule)(std::shared_ptr<Node>, std::shared_ptr<Node> );
int main(int, const char**)
{
std::vector<rule> v;
v.push_back(&A::rule);
return 0;
}
請發布一個完整的非編譯示例,您的問題必須隱藏在其他地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.