views:

103

answers:

2

I have an abstract base class which contains a private nested implementation. visual c++ is giving me the following error when I try to instantiate the non-abstract nested implementation:

error C2259: 'node::empty_node' : cannot instantiate abstract class (line 32)

as far as I can tell, I've overridden all the abstract members of the base class

Code follows:

using namespace boost;
template<typename K, typename V>
class node {
protected:
    class empty_node : public node<K,V> {
    public:
        bool is_empty(){ return true; }
        const shared_ptr<K> key() const { throw empty_node_exception; }
        const shared_ptr<V> value() const { throw empty_node_exception; }
        const shared_ptr<node<K,V>> left() const { throw empty_node_exception; }
        const shared_ptr<node<K,V>> right() const { throw empty_node_exception; }
        const shared_ptr<node<K,V>> add(const shared_ptr<K> &key, const shared_ptr<V> &value) const {
            return shared_ptr<node<K,V>>();
        }
        const shared_ptr<node<K,V>> remove(const shared_ptr<K> &key) const { throw empty_node_exception; }
        const shared_ptr<node<K,V>> search(const shared_ptr<K> &key) const { return shared_ptr<node<K,V>>(this); }
    };
    static shared_ptr<node<K,V>> m_empty;
public:
    virtual bool is_empty() = 0;
    virtual const shared_ptr<K> key() = 0;
    virtual const shared_ptr<V> value() = 0;
    virtual const shared_ptr<node<K,V>> left() = 0;
    virtual const shared_ptr<node<K,V>> right() = 0;
    virtual const shared_ptr<node<K,V>> add(const shared_ptr<K> &key, const shared_ptr<V> &value) = 0;
    virtual const shared_ptr<node<K,V>> remove(const shared_ptr<K> &key) = 0;
    virtual const shared_ptr<node<K,V>> search(const shared_ptr<K> &key) = 0;


    static shared_ptr<node<K,V>> empty(){
        if(NULL == m_empty.get()){
            m_empty.reset(new empty_node());
        }
        return m_empty;
    }
};
+6  A: 

The function signatures don't match. The pure virtual member functions in the base class 'node' are not const; the functions in the derived class 'empty_node' are const.

You either need to make the base class virtual functions const or remove the const qualifier from the member functions in the derived class.

James McNellis
+3  A: 

Your nested class is missing a non-const versions of key, value, left, right, add, remove and search methods.

Your const functions are not overrides.

Charles Bailey