c++11 - What is wrong with following C++ program? -


why following c++ code gives below mentioned error? why not idiomatic way of writing recursive data-structures in c++? there fundamentally wrong way of writing c++?

#include<iostream> using namespace std;   class tree{ public:     virtual void inorder() {}; };  class emp_tree: public tree{ public:     void inorder(){     } };  class non_emp_tree: public tree{ public:     tree left, right;     int val;     non_emp_tree(int v, tree l, tree r): val(v), left(l), right(r) {};     void inorder(){         left.inorder();         cout<<" "<<val<<" ";         right.inorder();     } };     int main() {     tree leaf1 = non_emp_tree(1, emp_tree(), emp_tree());     tree leaf2 = non_emp_tree(3, emp_tree(), emp_tree());     tree root = non_emp_tree(2, leaf1, leaf2);     root.inorder();     return 0; } 

error given compiler: (i'm unable comprehend of it)

/tmp/ccajhirw.o: in function `main': b_t.cpp:(.text+0x16e): undefined reference `tree::inorder()' /tmp/ccajhirw.o: in function `tree::tree()': b_t.cpp:(.text._zn4treec2ev[_zn4treec5ev]+0x9): undefined reference `vtable tree' /tmp/ccajhirw.o: in function `tree::tree(tree const&)': b_t.cpp:(.text._zn4treec2erks_[_zn4treec5erks_]+0xd): undefined reference `vtable tree' /tmp/ccajhirw.o: in function `non_emp_tree::inorder()': b_t.cpp:(.text._zn12non_emp_tree7inorderev[_zn12non_emp_tree7inorderev]+0x19): undefined reference `tree::inorder()' b_t.cpp:(.text._zn12non_emp_tree7inorderev[_zn12non_emp_tree7inorderev]+0x56): undefined reference `tree::inorder()' /tmp/ccajhirw.o: in function `tree::tree(tree&&)': b_t.cpp:(.text._zn4treec2eos_[_zn4treec5eos_]+0xd): undefined reference `vtable tree' /tmp/ccajhirw.o:(.rodata._zti12non_emp_tree[_zti12non_emp_tree]+0x10): undefined reference `typeinfo tree' /tmp/ccajhirw.o:(.rodata._zti8emp_tree[_zti8emp_tree]+0x10): undefined reference `typeinfo tree' collect2: error: ld returned 1 exit status 

edit: changed virtual void inroder() virtual void inorder() {} i.e empty implementation. still not getting desired output, seems root, leaf1 , leaf2 both calling tree's inorder not respective inorders.

you never implemented tree::inorder.

class tree{ public:     virtual void inorder(); }; 

here claimed there such function -- implementation?

also, doesn't make sense:

tree leaf1 = non_emp_tree(1, emp_tree(), emp_tree()); 

you're setting tree's value equal non_emp_tree's value. won't useful.


Comments