Reputation: 13
I want to define a binary tree class template like this:
template<typename Any>
class tree {
public:
struct treeNode {
Any &_data;
treeNode *_left, *_right;
treeNode(const Any &data, treeNode *left = nullptr,
treeNode *right = nullptr) :
_data(data), _left(left), _right(right) {
}
};
// ...
std::vector<treeNode *> lookfor(
const treeNode *, const std::function<bool(const Any &)> &) const;
};
// ↑ tree.h
// ↓ tree.cpp
template<typename Any>
std::vector<tree::treeNode *> tree<Any>::lookfor(
const treeNode *x, const std::function<bool(const Any &)> &cmp) const {
// ...
}
Then g++ was not happy and told me this:
tree.cpp:129:29: error: template argument 1 is invalid
std::vector<tree::treeNode *> tree<Any>::lookfor(
^
tree.cpp:129:29: error: template argument 2 is invalid
tree.cpp:129:31: error: prototype for ‘int tree<Any>::lookfor(const tree<Any>::treeNode*, const std::function<bool(const Any&)>&) const’ does not match any in class ‘tree<Any>’
std::vector<tree::treeNode *> tree<Any>::lookfor(
^
In file included from tree.cpp:1:0:
tree.h:88:33: error: std::vector<tree<Any>::treeNode*> tree<Any>::lookfor(const tree<Any>::treeNode*, const std::function<bool(const Any&)>&) const
std::vector<treeNode *> lookfor(
^
So I wonder why it happened and how I can fix this bug.
I'm not good at English and this is my first time asking a question on statckoverflow so if I have made any mistake please tell me :)
Upvotes: 1
Views: 1522
Reputation: 576
The code:
std::vector<tree::treeNode *> tree<Any>::lookfor
Is referencing tree, which is a template, without template arguments. It should be something like:
std::vector<typename tree<Any>::treeNode *> tree<Any>::lookfor
Also, as these are templates you shouldn't have a .cpp file for tree, that is also causing problems.
Upvotes: 5