有人可以解釋或幫助我爲什麼這不起作用嗎?C++中的Lambda表達式std :: copy_if
std::vector<std::shared_ptr<Publication> > Bibliography::givePubWithIFHigherThan(float value) const
{
Publication *p;
std::vector<std::shared_ptr<Publication>> highIFPubs(publications);
auto checkIF = std::mem_fun(p->IFHigherThan(value));
auto last = std::copy_if(publications.begin(), publications.end, highIFPubs.begin(),
[=] (std::shared_ptr<Publication> p)
{
return checkIF(*p, value);
});
return highIFPubs;
}
class Publication
{
public:
Publication(std::string aTitle, int aYear, std::string anID);
virtual bool IFHigherThan(float value) const {return false;};
private:
};
class Paper : public Publication
{
public:
Paper(std::string aTitle, int aYear, std::string aJournal, float aImpactFactor);
bool IFHigherThan(float value) const {return value < impactFactor;};
private:
};
目前,我得到這個錯誤,
no matching function for call to 'mem_fun(bool)' auto checkIF = std::mem_fun(p->IFHigherThan(value)); ^
這是因爲你正在調用IFHigherThan函數,而不是傳遞指針。 –
但是p是一個指針吧? – user4390280
是的,'p'是一個指針,但是'p-> IFHigherThan(value)'是一個函數調用,導致一個'bool'。 –