Salve a tutti!
Ho un problema innanzitutto ecco il codice:
codice:
struct element{
size_type row;
size_type col;
value_type val;
};
class iterator {
friend class const_iterator;
friend class sparsematrix;
public:
typedef std::forward_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
iterator() : ptr(0),ptrc(0),ptrr(0) {}
iterator(const iterator &other) : ptr(other.ptr),ptrc(other.ptrc),ptrr(other.ptrr) {}
iterator& operator=(const iterator &other) {
ptr = other.ptr;
ptrc = other.ptrc;
ptrr = other.ptrr;
return *this;
}
iterator& operator=(const element &other) {
element el;
el.row = *ptrr;
el.col = *ptrc;
el.val = *ptr;
return ⪙
}
~iterator() {}
/**
* Operatore di dereferenziamento.
* @return struct element
*/
reference operator*() const {
return *ptr;
}
/**
* Operatore freccia.
*
* @return il puntatore al dato "puntato" dall'iteratore.
*/
pointer operator->() const {
element *el;
el.row = *ptrr;
el.col = *ptrc;
el.val = *ptr;
return el;
}
/**
* Operatore di confronto (uguaglianza).
*
* @param other iteratore da confrontare
* @return true se i due iteratori "puntano" allo stesso dato
*/
bool operator==(const iterator &other) const {
return ptr == other.ptr;
}
/**
* Operatore di confronto (disuguaglianza).
*
* @param other iteratore da confrontare
* @return true se i due iteratori "puntano" a dati diversi
*/
bool operator!=(const iterator &other) const {
return !(*this == other);
}
/**
* Operatore di confronto (uguaglianza).
*
* @param other const_iteratore da confrontare
* @return true se i due iteratori "puntano" allo stesso dato
*/
bool operator==(const const_iterator &other) const {
return ptr == other.ptr; // vedi const_iterator
}
/**
* Operatore di confronto (uguaglianza).
*
* @param other const_iteratore da confrontare
* @return true se i due iteratori "puntano" a dati diversi
*/
bool operator!=(const const_iterator &other) const {
return !(*this == other);
}
/**
* Operatore di "elemento successivo". Versione pre-incremento.
*
* @return l'iteratore che "punta" all'elemento successivo
*/
iterator &operator++() {
++ptr;
++ptrc;
++ptrr;
return *this;
}
/**
* Operatore di "elemento successivo". Versione post-incremento.
*
* @return l'iteratore che "punta" all'elemento successivo
*/
iterator operator++(int) {
iterator tmp(ptr,ptrc,ptrr);
++ptr;
++ptrc;
++ptrr;
return tmp;
}
private:
value_type *ptr;
size_type *ptrc;
size_type *ptrr;
/**
* Costruttore di inizializzazione.
* @param p puntatore ai dati
* @param ptrc puntatore alle colonne
* @param ptrr puntatore alle righe
*/
explicit iterator(value_type *p, size_type *prc, size_type *prr) : ptr(p), ptrc(prc),ptrr(prr){}
}; // iterator
iterator begin() {
return iterator(val,pc,pr);
}
iterator end() {
return iterator(val + size, pc + size, pr + size);
}
}
Allora sostanzialmente devo far si che il mio iteratore ritorni una struct e che poi io la possa utilizzare per qualcosa.
Qualunque cosa io faccia lui non mi fa usare la struct, secondo voi cosa devo fare? cosa ho sbagliato?
Per completezza inserisco anche il pezzo di codice dove devo usarla:
codice:
sparsematrix<int> sit(2,2,0);
sit.add(0,0,4);
sit.add(1,1,3);
sparsematrix<int>::element *el;
sparsematrix<int>::iterator i = sit.begin();
i->el;