I wanted to implement a simple binary tree in which the vertices hold lambdas.
class Element {
public:
Element() { }
virtual int operator()();
};
class Node : public Element {
private:
std::function<int(Element, Element)> fn;
Element left;
Element right;
public:
Node(std::function<int(Element, Element)> _fn, Element _left, Element _right)
: fn(_fn), left(_left), right(_right) { }
int operator()() { return fn(left, right); }
};
class Leaf : public Element {
private:
int value;
public:
Leaf(int _value) : value(_value) { }
int operator()() { return value; }
};
int main() {
cout << Node([](Element a, Element b) { return a() + b(); }, Leaf(2), Leaf(5))() << endl;
return 0;
}
And here are errors which I'm getting:
g++ -Wall -std=c++11 -Wextra -pedantic tree.cc -c
g++ -Wall -std=c++11 -Wextra -pedantic tree.o -o tree
tree.o: In function `main::{lambda(Element, Element)#1}::operator()(Element, Element) const':
tree.cc:(.text+0x18): undefined reference to `vtable for Element'
tree.cc:(.text+0x2a): undefined reference to `vtable for Element'
tree.o: In function `Element::Element()':
tree.cc:(.text._ZN7ElementC2Ev[_ZN7ElementC5Ev]+0xf): undefined reference to `vtable for Element'
tree.o: In function `Element::Element(Element const&)':
tree.cc:(.text._ZN7ElementC2ERKS_[_ZN7ElementC5ERKS_]+0x13): undefined reference to `vtable for Element'
tree.o: In function `Node::Node(std::function<int (Element, Element)>, Element, Element)':
tree.cc:(.text._ZN4NodeC2ESt8functionIFi7ElementS1_EES1_S1_[_ZN4NodeC5ESt8functionIFi7ElementS1_EES1_S1_]+0x4e): undefined reference to `vtable for Element'
tree.o:tree.cc:(.text._ZN4NodeC2ESt8functionIFi7ElementS1_EES1_S1_[_ZN4NodeC5ESt8functionIFi7ElementS1_EES1_S1_]+0x5a): more undefined references to `vtable for Element' follow
tree.o:(.rodata._ZTI4Leaf[_ZTI4Leaf]+0x10): undefined reference to `typeinfo for Element'
tree.o:(.rodata._ZTI4Node[_ZTI4Node]+0x10): undefined reference to `typeinfo for Element'
collect2: error: ld returned 1 exit status
make: *** [tree] Error 1
What does it mean, is there a problem with classes declarations or I do not understand some aspects of functional programming in C++?