我有一个模板类,有一个像这样的嵌套模板类
#include <utility>
template<typename K> class Tree23 {
public:
template<typename Key> class Node {
private:
friend class Tree23<K>;
// snip . . .
};
Node<K> root;
public:
// snip ...
std::pair<bool, Node<K> *> Search(K key);
};
我在Search方法实现的签名上得到了几个编译错误
template<typename K>
std::pair<bool, Tree23<K>::Node<K> *> Tree23<K>::Search(K key)
{
// make sure tree has at least one element
if (root == 0) {
return std::make_pair(false, 0);
} else {
return Locate(key, root);
}
}
错误对应于
行template<typename K> std::pair<bool, Tree23<K>::Node<K> *> Tree23<K>::Search(K key)
complie错误是:
Node.h:64:55: error: type/value mismatch at argument 2 in template parameter list for 'template<class _T1, class _T2> struct std::pair'
Node.h:64:55: error: expected a type, got '(Tree23<K>::Node < <expression error>)'
Node.h:64:58: error: expected unqualified-id before '>' token
Node.h:64:58: error: expected initializer before '>' token
我不清楚如何解决这个问题。任何反馈都将不胜感激。
答案 0 :(得分:2)
试试这个:
template<typename K>
std::pair<bool, typename Tree23<K>::template Node<K> *> Tree23<K>::Search(K key)
// ^^^^^^^^ ^^^^^^^^
{
// make sure tree has at least one element
if (root == 0) {
return std::make_pair(false, 0);
} else {
return Locate(key, root);
}
}
同样check this out为什么这是必要的。