继承带有内部类的模板类,并在继承的类中访问内部类
我有一个模板类“BinaryHeap”,它还在其内部声明了一个公共类“Item”。
现在我想使用用于元素查找的哈希来扩展 BinaryHeap 并因此继承它。我将其称为“HashedBinaryHeap”,它应该使用与 BinaryHeap 相同的 Item 类。
存根看起来像这样:
template<class T>
class BinaryHeap {
public:
class Item {...};
...
void appendItem(const Item & item);
...
};
template<class T>
class HashedBinaryHeap : public BinaryHeap<T> {
public:
...
void appendItem(const Item & item);
...
};
现在的问题是,当我尝试像在 appendItem()
方法中那样访问 HashedBinaryHeap 中的 Item 类时,我收到一些编译器错误。
当我像上面那样或使用
void appendItem(const Item & item);
void appendItem(const Item<T> & item);
我得到:
ISO C++ forbids declaration of 'Item' with no type
当我执行以下操作之一时:
void appendItem(const HashedBinaryHeap::Item & item);
void appendItem(const HashedBinaryHeap<T>::Item & item);
我得到:
expected unqualified-id before '&' token
那么我如何“访问”HashedBinaryHeap 中的类 Item?我有什么误解吗?
(也许这不是与模板类相关的问题,但我知道模板类让很多 C++ 初学者感到困惑,而且我仍然不敢称自己为其他东西......请让我出去。:))
提前致谢!
I have a template class "BinaryHeap" which also declares a public class "Item" within itself.
Now I want to extend the BinaryHeap with a hash for element lookup and therefore inherited it. I called it "HashedBinaryHeap", which should use the same Item class like BinaryHeap does.
The stub looks like this:
template<class T>
class BinaryHeap {
public:
class Item {...};
...
void appendItem(const Item & item);
...
};
template<class T>
class HashedBinaryHeap : public BinaryHeap<T> {
public:
...
void appendItem(const Item & item);
...
};
The problem now is, when I try to access the Item class within HashedBinaryHeap like I do in the appendItem()
method, I get some compiler errors.
When I write it like above or with the <T>:
void appendItem(const Item & item);
void appendItem(const Item<T> & item);
I get:
ISO C++ forbids declaration of 'Item' with no type
When I do one of:
void appendItem(const HashedBinaryHeap::Item & item);
void appendItem(const HashedBinaryHeap<T>::Item & item);
I get:
expected unqualified-id before '&' token
So how can I 'access' the class Item within HashedBinaryHeap? What am I misunderstanding?
(Maybe this isn't something template class related problem, but I know template classes are confusing a lot of C++ beginners, and I still don't dare calling myself something else... Please get me out. :))
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,解决了!
这成功了 - 我不知道 typename...
Ok, solved it!
This did the trick - I didn't know about typename...