templates - C++ Invalid use of 'this' in non-member function -
this question has answer here:
i have error "invalid use of 'this' in non-member function"
what correct way write code avoid error.
tree.h:
#ifndef tree_h #define tree_h template <typename t> class node; class tree { public: tree(); template <typename tnodetype> node<tnodetype> elaborate(node<tnodetype> &node); tree* self(); void dosomething(); }; template <typename tnodetype> node<tnodetype> createnew() { node<tnodetype> model(this); //<-- error here return model; } #endif // tree_h
node.h:
#ifndef node_h #define node_h #include <tree.h> template <typename tnodetype> class node { public: node(tree *tree); tnodetype current(); private: tree *_tree; }; template <typename tnodetype> node<tnodetype>::node(tree *tree): _tree(tree) { _tree->dosomething(); } template <typename tnodetype> tnodetype node<tnodetype>::current() { //some code here } #endif // node_h
solved.
in tree.h skipped declaration of:
template <typename tnodetype> node<tnodetype> createnew();
and had forgotten in definition "tree::" before "createnew()"
i agree question have been avoided ;-). sorry
createnew()
free function (i.e. not member of class), , therefore has no notion of this
.
Comments
Post a Comment