考慮下面的代碼:訪問二叉搜索樹的功能
#include "ClassB.h"
ClassA
{
private:
Vector<std::string> idVec;
public:
int getTotal(ClassB& (*func) (std::string));
}
int ClassA::getTotal(ClassB& (*func) (std::string))
{
int total = 0;
for (int i:0; i < idVec.Size(); i++)
{
total += (*func) (idVec[i]).GetInt();
}
return total;
}
ClassB
{
private:
string id;
int i;
public:
std::string getId();
int getInt();
}
ClassB& accessTree(stD::string id);
main()
{
BSTree<ClassB> //Binary Search Tree
ClassA a;
//Assume id's get set.
a.getTotal(accessTree);
}
ClassB& accessTree(stD::string id)
{
//This is part I'm not 100% on.
}
ClassB的的運營商已經超載。如果你願意的話,請考慮它是主鍵。
**編輯 感謝Joachim Pileborg我開始使用/瞭解佔位符和綁定。
現在我要發佈的是我的實際實現,但概念是相同的。單位= ClassB。 R(aka註冊)= ClassA。
調用功能
template<typename Ft>
unsigned Registration::GetCredits(Ft func)
{
unsigned sum = 0;
for(unsigned i = 0; i < GetSize(); i++)
sum += results[i].GetCredits(func(results[i].GetUnit()));
return sum;
}
Unit& accessTree(std::string id, BSTree<Unit>& tree)
{
Unit tempU;
tempU.SetId(id);
return tree.search(tempU);
}
在主
R.GetCredits(std::bind(accessTree, _1, std::ref(uTree)));
未定義參考`無符號整型登記:: GetCredits(STD :: _佔位< 1>,的std ::的reference_wrapper>))(STD :: std :: string,BSTree &)>>(std :: _ Bind(std :: _佔位符< 1>,std :: reference_wrapper>))(std :: string,BSTree &)>)'|
有點卡在這一點上,我錯過了什麼?
在此先感謝。
嘿感謝約阿希姆, 我有一個關於佔位符和std ::綁定和未讀100%確定它做了什麼? 你能給出一個很菜鳥的解釋嗎?或另一個例子? –