开发者

Inherit a template class with an inner class and access the inner class within the inherited class

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 mis开发者_JAVA百科understanding?

(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!


Ok, solved it!

void appendItem(const typename HashedBinaryHeap<T>::Item & item);

This did the trick - I didn't know about typename...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜