Reputation: 594
I have some code that accepts a pointer to an object, and this has been working fine thus far, however, after some cleanup (I code messy, and then clean up when most of the bugs are gone), a function that accepts a pointer, is now treating the pointer passed to it as an int*
.
I have checked that I am indeed passing a pointer, and can't seem to figure this one out. Why is this happening?
Here's some code below (highly trimmed down, and I am not sure if I'm allowed to even show a small amount of this code, but I need to figure this out, so sorry if it doesn't make a lot of sense). I've removed sensitive parts of the code, and frankly, parts I feel I'd be in trouble for posting. But the erroneous part of the code was included.
template <class T>
class TreeStructure {
class TreeNode {
public:
T data;
TreeNode<T> * parent;
TreeNode<T> * left;
TreeNode<T> * right;
inline TreeNode(T d) : data(d), parent(0x0), left(0x0), right(0x0) {}
inline TreeNode(T d,TreeNode<T>* p) : data(d), parent(p), left(0x0), right(0x0) {}
inline TreeNode(T d,TreeNode<T>* p,TreeNode<T>* l,TreeNode<T>* r) : data(d), parent(p), left(l), right(r) {}
};
TreeNode<T>* root;
// Recursive CRUD operations, used as helpers to public CRUD operations, _delete shown
inline void _delete(TreeNode<T>* c) {
if(c != 0x0) {
_delete(c->left); // This is where things go wrong, _delete compains of no valid conversion from 'int*' to 'TreeStructure::TreeNode<T>* [with T = int]'
_delete(c->right);
delete c;
_size--;
// Irrelevant operations down here
}
}
public:
inline bool remove(T d) {
// Check if 'd' is in tree, return false if not, get pointer to containing node if so
TreeNode<T>* node; // This is where we get the reference to the node. Code returns if data isn't found, so if we're here, we know we have a node.
// _delete is never called if 'd' isn't in the tree, so we can assume its set to a node since we got this far
_delete(node); // This call is fine, but the recursive calls in _delete fail
}
};
When I call remove()
, and remove()
in turn calls _delete()
, which complains that the node is an int*
when it is a TreeNode<int>*
. And I know that whatever is passed to _delete()
is a TreeNode<int>*
, and even when recursively called, is guarded against NULL (noop if null). Why is this happening and how do I solve the issue?
Upvotes: 0
Views: 119
Reputation: 169008
The compiler doesn't understand what TreeNode<T>
is because it doesn't name a type; TreeNode
is not a template. Use TreeNode
everywhere you have TreeNode<T>
.
TreeNode
implicitly gets T
from its outer template class, so specifying it in this way isn't even necessary. To put it another way, TreeStructure<int>::TreeNode
and TreeStructure<std::string>::TreeNode
are already different types.
I'm not sure why you get the specific error you get, but I would assume that somewhere in the mess of errors the compiler spit out is it complaining that TreeNode
is not a template and so the <T>
suffix doesn't make sense. It's a longstanding tradition that C and C++ compilers try to forge ahead even after encountering an error and try to make things work. I think the theory is that the more errors it can raise at once, the fewer compiler invocations you need to make because you could fix multiple errors each run. In practice, however, it means the compiler is making bad assumptions and then generating errors predicated on those assumptions, so it can also generate a lot of noise.
In C and C++ programming, always pay attention to the first error. Everything after that is likely to be useless noise.
Upvotes: 2