2013-03-08 61 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