1

ええと、私には3つのクラスがあります。

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:すべてのSetのメソッドが実装されています。

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はaddメソッドを実装していません。これは、Collection :: add(const Collection&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-> add(* s);」の行で次のようなエラーを受け取ります:「SortedSet :: add(Set&)」に一致しません</ p>

なぜこうなった?

4

1 に答える 1

1

これで、関連するすべてのコードを投稿しました。問題は、同じ名前の別の関数を次のように宣言したことですSet

virtual void add(const E & value);

これにより、基本クラスで同じ名前のすべてのものが非表示になります。そのため、そのサブクラスへの参照またはそのサブクラスCollection::addを介してアクセスすることはできません。Set

これを修正するには、using-declarationをに追加してSet、:Collection::addのスコープに追加します。Set

public:
    using Collection::add;
于 2013-03-08T05:43:56.917 に答える