2013-03-08 57 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()); 
} 

} 

觀測值:所有設置的方法來實現。

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(); 
    } 
}; 

集和SortedSet的不實現添加方法,因爲他們應該怎麼做收藏::添加(常量集合& c)作出。

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; 
} 

但是這個代碼得到錯誤行 「SS->添加(* S);」 曰:不匹配 '的SortedSet ::添加(設置&)'

這是爲什麼發生了什麼?

+0

至於我可以告訴這個代碼工作http://liveworkspace.org/code/ LW251 $ 2 – 2013-03-08 04:50:05

+0

它編譯罰款給我。 http://ideone.com/a58gi2 – 2013-03-08 04:50:54

+0

[我不明白](http://ideone.com/S5yQkR)。這是你試圖編譯的確切代碼嗎?你正在使用哪種編譯器? – 2013-03-08 04:51:01

回答

1

現在你已經發布的所有相關代碼,問題是,你聲明具有相同名稱的另一個功能Set

virtual void add(const E & value); 

這個隱藏在基類中的同名任何東西;因此Collection::add不能通過對Set或其任何子類的引用訪問。

爲了解決這個問題,添加一個using聲明到Set,帶來Collection::addSet範圍:

public: 
    using Collection::add; 
+0

現在,現在我得到錯誤消息: '無效收集 ::添加(常量收集&)與E = INT]' 不可訪問 Lpoo.cpp:26:12:錯誤:在這種情況下 – 2013-03-08 06:06:38

+0

@BrunoGouveia:我猜你已將私有或受保護的使用聲明。嘗試公開。 – 2013-03-08 06:13:15

+0

我得到了,我正在使用Collection ::添加錯誤的地方。謝謝你,你真的幫了忙。但是我不明白到底發生了什麼,你能更好地解釋我嗎? – 2013-03-08 06:14:44