2013-03-08 5 views
1

Ну, у меня есть три класса:Асесс прародитель виртуальный метод

template<typename E> 
class Iterator { 
public: 
    virtual ~Iterator() { 
    } 
    virtual bool hasNext() const = 0; 
    virtual const E& next() = 0; 
}; 

template<typename E> 
class IteratorPtr { 
private: 
    Iterator<E>* iterator; 
    IteratorPtr(const IteratorPtr<E>&); 
    IteratorPtr<E>& operator=(const Iterator<E>&); 
public: 
    IteratorPtr(Iterator<E>* it) 
      : iterator(it) { 
    } 
    ~IteratorPtr() { 
     delete iterator; 
    } 
    Iterator<E>* operator->() const { 
     return iterator; 
    } 
};  

template<typename E> 
class Collection 
{ 

public: 
    virtual void add(const E & value) = 0; 
    virtual void add(const Collection<E>& collection); 
    virtual bool remove(const E& value) = 0; 
    virtual void clear() = 0; 
    virtual ~Collection() 
    { 

    } 
    virtual bool isEmpty() const = 0; 
    virtual int size() const = 0; 
    virtual bool contains(const E& value) const = 0; 
    virtual Iterator<E>* iterator() const = 0; 

}; 

void Collection<E>::add(const Collection<E>& collection) 
{ 
for (IteratorPtr<E> i(collection.iterator()); i->hasNext();) { 
    this->add(i->next()); 
} 

} 

Obs: методы все Сета реализуются.

template<typename E> 
class Set; 
template<typename E> 
class SortedSet; 
template<typename E> 
class SetIterator; 

template<typename E> 
class SetNode { 
private: 
    friend class Set<E> ; 
    friend class SortedSet<E> ; 
    friend class SetIterator<E> ; 
    SetNode() 
      : next(NULL) { 
    } 
    SetNode(E value) 
      : value(value), next(NULL) { 
    } 
    SetNode(E value, SetNode * next) 
      : value(value), next(next) { 
    } 
    ~SetNode() { 
    } 
private: 
    E value; 
    SetNode * next; 
}; 

template<typename E> 
class SetIterator: public Iterator<E> { 
private: 
    friend class Set<E> ; 
    SetIterator(SetNode<E> * head) 
      : node(head) { 
    } 
    ~SetIterator() { 
    } 
    bool hasNext() const { 
     return node != NULL; 
    } 
    const E & next() { 
     E& value = node->value; 
     node = node->next; 
     return value; 
    } 

private: 
    SetNode<E> * node; 
}; 

template<typename E> 
class Set: public Collection<E> { 

public: 
    Set(): numNodes(0), head(NULL) { 
    } 
    virtual ~Set() { 
     clear(); 
    } 
    virtual void add(const E & value); 
    virtual bool remove(const E& value); 
    virtual void clear(); 
    virtual bool isEmpty() const; 
    virtual int size() const; 
    virtual bool contains(const E& value) const; 
    virtual Iterator<E>* iterator() const; 
private: 
    Set(const Set & obj): numNodes(0), head(NULL) { 
    } 
    virtual bool contains(const E & value, SetNode<E> * & previ) const; 
protected: 
    int numNodes; 
    SetNode<E> * head; 

}; 
template<typename E> 
class SortedSet: public Set<E> { 

private: 
    virtual bool contains(const E& value, SetNode<E> *& prev) const; 

public: 
    SortedSet(): Set<E>() { 
    } 
    virtual void add(const E & value); 
    virtual ~SortedSet() { 
     this->clear(); 
    } 
}; 

Set и SortedSet не реализуют добавить методы, потому что они должны делать то, что коллекция :: добавить (константный Коллекция & с) делает.

int main() { 
Set<int> *s = new Set<int>(); 
s->add(10); 
s->add(30); 
s->add(12); 
s->remove(10); 
if (s->contains(30)) 
    puts("Tem"); 
SortedSet<int> *ss = new SortedSet<int>(); 
ss->add(*s); 

return 0; 
} 

но этот код получить ошибку в строке "& beta;> добавить (* s);", говоря: нет соответствия в 'SortedSet :: добавить (Set &)'

Почему это происходит?

+0

Насколько я могу сказать, этот код работает http://liveworkspace.org/code/ LW251 $ 2 –

+0

Он компилируется для меня. http://ideone.com/a58gi2 –

+0

[Я не получаю эту ошибку] ​​(http://ideone.com/S5yQkR). Это точный код, который вы пытаетесь скомпилировать? Какой компилятор вы используете? –

ответ

1

Теперь вы опубликовали все соответствующий код, проблема заключается в том, что вы объявили другую функцию с тем же именем в Set:

virtual void add(const E & value); 

Это скрывает что-нибудь с таким же именем в базовом классе; поэтому Collection::add недоступен по ссылке Set или любому из его подклассов.

Чтобы это исправить, добавьте используя декларацию в Set, чтобы привести Collection::add в сферу Set:

public: 
    using Collection::add; 
+0

сейчас, теперь я получил сообщение об ошибке: 'void Collection :: add (const Collection &) [with E = int]' недоступно Lpoo.cpp: 26: 12: error: в этом контексте –

+0

@BrunoGouveia: Я предполагаю, что вы использовали служебную декларацию как частную или защищенную. Попробуйте сделать его общедоступным. –

+0

Я получил, я использовал с помощью Collection :: add в неправильном месте. Спасибо, чувак, ты действительно помог. Но я не понял, что именно произошло, не могли бы вы объяснить мне лучше? –