C++11 c+中的lambda函数+;继承 我对C++中的lambda函数是新的,我试图做一个简单的但有一些问题。我曾尝试创建一个包含堆栈、队列和列表的异构容器,其中一个练习是创建一个lambda函数,用于检查元素是否满足以下特定条件: using Condition = bool (*)(T const&);
这是我的异构容器的一部分: 例如,对于堆栈:C++11 c+中的lambda函数+;继承 我对C++中的lambda函数是新的,我试图做一个简单的但有一些问题。我曾尝试创建一个包含堆栈、队列和列表的异构容器,其中一个练习是创建一个lambda函数,用于检查元素是否满足以下特定条件: using Condition = bool (*)(T const&);,c++11,lambda,C++11,Lambda,这是我的异构容器的一部分: 例如,对于堆栈: template <typename T> using Condition1 = bool (*)(T const&); template <typename T> class LinkedStack { private: StackElement<T>* top; public: LinkedStack(); LinkedStack(LinkedStack const&
template <typename T>
using Condition1 = bool (*)(T const&);
template <typename T>
class LinkedStack {
private:
StackElement<T>* top;
public:
LinkedStack();
LinkedStack(LinkedStack const&);
LinkedStack& operator=(LinkedStack const&);
bool empty() const;
bool member(T const& x);
T peek() const;
void push(T const&);
T pop();
~LinkedStack();
};
template<typename T,typename Condition1>
bool q_filter(Condition1 func,LinkedStack<T>& s){
LinkedStack<T> tmp;
tmp = s;
if((tmp).empty())
return false;
while(!tmp.empty()){
if (func(tmp.peek()))
return true;
else
tmp.pop();
}
return false;
}
QueueStackList的实现类似于链表,qsl.begin()返回
异类列表中第一个元素的迭代器;
编译时,它会返回此类错误:
invalid user-defined conversion from 'main()::<lambda(int)>' to 'Object<int>::Condition {aka bool (*)(const int&)}' [-fpermissive]|
candidate is: main()::<lambda(int)>::operator bool (*)(int)() const <near match>|
no known conversion for implicit 'this' parameter from 'bool (*)(int)' to 'Object<int>::Condition {aka bool (*)(const int&)}'|
从'main()::'到'Object::Condition{aka bool(*)(const int&)}'[-fppermissive]的用户定义转换无效|
候选对象是:main()::运算符bool(*)(int)()const|
隐式'this'参数从'bool(*)(int)'到'Object::Condition{aka bool(*)(const int&)}没有已知的转换|
我真的不知道这是什么意思 您正试图将接受
int
参数的lambda传递给期望函数接受int&
的被调用方。当我更改为int-const&时,它返回以下错误:| |===未识别的对` Object::special_condition(bool()(int-const&)')的引用,我不理解为什么条件为bool()(t-const&)在heterogenius上下文中,我定义了stack对象,比如stackobject,实际上,在您展示的代码中,没有任何地方实现了object::special_condition
。你的意思是让它成为纯虚拟的吗,就像你对对象的其他成员那样
?在StackObject中的函数排序之后,我有:bool-special\u条件(条件c){return q\u-filter(c,*this);}。是的,你实现了StackObject::sepical\u条件
,但没有对象::special\u条件
。
int main(){
QueueStackList qsl;
qsl.read_from_file();
(*(qsl.begin()))->special_condition([](int x) -> bool { return x%2 != 0; });
return 0;
}
invalid user-defined conversion from 'main()::<lambda(int)>' to 'Object<int>::Condition {aka bool (*)(const int&)}' [-fpermissive]|
candidate is: main()::<lambda(int)>::operator bool (*)(int)() const <near match>|
no known conversion for implicit 'this' parameter from 'bool (*)(int)' to 'Object<int>::Condition {aka bool (*)(const int&)}'|