mercredi 29 juin 2016

Compiler doesn't recognize a copy constructor of a class within a class


I've written code for a tuple class (of integers) which also defines an iterator class for iterating over the tuple. The iterator has two elements- a pointer to the tuple it originated from and an integer denoting the current position of the iterator. However, the compiler doesn't recognize the copy constructor for the iterator (whether I use the default one or write one myself). Here's the code: http://ideone.com/nVNDvC or if the link doesn't work:

class tuple {
    int* array;
    int size;
public:
    tuple(int* a, int s) : array(new int[s]), size(s) {
        for(int i=0 ; i<s; ++i)
            array[i]=a[i];
    }
    ~tuple() { delete[] array;}
    tuple (const tuple& t) : tuple(t.array, t.size) { };

    class iterator {
        tuple* origin;
        int index;
        iterator(tuple* o, int i) : origin(o), index(i) { }
        friend class tuple;
    public:
        iterator(const iterator&) = default;
    };
    iterator begin() { return iterator(this, 0); }
    iterator end() { return iterator(this, size); }
};

int main() {
    int myarray[2] = {42, 43};
    tuple mytuple(myarray, 2);
    tuple::iterator iterator1 = mytuple.begin();
    tuple::iterator iterator2 = mytuple.end();
    iterator2(iterator1);
    return 0;
}

I get the error:

prog.cpp: In function 'int main()':
prog.cpp:29:21: error: no match for call to '(tuple::iterator) (tuple::iterator&)'
  iterator2(iterator1);

Aucun commentaire:

Enregistrer un commentaire