【发布时间】:2014-12-10 18:07:44
【问题描述】:
我想像这样定义一个二叉树类模板:
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 {
// ...
}
然后g++不高兴了,告诉我这个:
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(
^
所以我想知道为什么会发生这种情况以及如何修复这个错误。
我不擅长英语,这是我第一次在 statckoverflow 上提问,如果我犯了任何错误,请告诉我:)
【问题讨论】:
-
旁注:
Any &_data初始化为const Any &data