I have the error "invalid use of 'this' in non-member function"
What is the correct way to write the code to avoid this 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 I skipped the declaration of:
template <typename TNodeType>
Node<TNodeType> createNew();
And I had forgotten in the definition "Tree::" before "createNew()"
I agree that this question could have been avoided ;-). Sorry