Ho la seguente classe Esercizio (commerciale)
codice:
#ifndef ESER_H
#define ESER_H
#include <string>
class Esercizio
{
public:
//Constructors
Esercizio(const char * iva=" ", const char * comune=" ", int mtq=0) :
P_IVA(new char [strlen(iva)+1]),
Comune(new char [strlen(comune)+1]),
mq(mtq)
{
strcpy(P_IVA,iva);
strcpy(Comune,comune);
}
Esercizio(const Esercizio & E) :
P_IVA(new char [strlen(E.getP_IVA())+1]),
Comune(new char [strlen(E.getComune())+1]),
mq(E.getmq())
{
strcpy(P_IVA,E.getP_IVA());
strcpy(Comune,E.getComune());
}
//Distructor
~Esercizio();
//Getters
char * getComune() const {return Comune;}
char * getP_IVA() const {return P_IVA;}
int getmq() const {return mq;}
//Other functions
bool licenza(const int) const;
void VisualizzaDati() const;
protected:
char * P_IVA;
char * Comune;
int mq;
};
#endif
Purtroppo devo necessariamente gestire le variabili membro come char *, altrimenti avrei usato delle string.
Ad ogni modo, ho questa classe template:
codice:
#ifndef LIS_H
#define LIS_H
#include <iostream>
template <class E> class Lista;
template <class E>
class Record
{
friend class Lista<E>;
template <class U> friend std::ostream & operator << (std::ostream &, const Lista<U> &);
private:
E elem;
Record<E> * punt;
};
template <class E>
class Lista
{
private:
typedef Record<E> * L;
L l;
public:
Lista() : l(0) {}
~Lista();
bool empty() const {return (l==0);}
bool full() const {return false;}
void push(const E);
void top(E &) const;
void pop(E &);
void elimina(const E e) {eliminaric(l,e);}
void inserisci(const E e) {inserisciric(l,e);}
bool inlist(const E e) const {return inlistric(l,e);}
template <class U> friend std::ostream & operator << (std::ostream &, const Lista<U> &);
private:
void elimina_in_testa(L &);
void inserisciric(L &, const E);
void eliminaric(L &, const E);
bool inlistric(const L &, const E);
};
template <class E> Lista<E>::~Lista()
{
L p,q;
for (p=l; p!=0; p=q)
{
q=p->punt;
delete p;
}
}
template <class E> void Lista<E>::push(const E e)
{
L q = new Record<E>;
q->elem=e;
q->punt=l;
l=q;
}
#endif
Se dichiaro nel main un oggetto di tipo Lista<Esercizio> L, la compilazione avviene senza errori, ma in esecuzione il programma da il seguente errore:
codice:
MacBookPro:Esercizio Commerciale yuri$ ./yuri
yuri(1486) malloc: *** error for object 0x1001000c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap
Dai vari test che ho eseguito sono certo quasi al 100% che l'errore avviene in corrispondenza dell'assegnazione che ho evidenziato in rosso. (Nel corpo della funzione Lista<E>::push)
PS Se occorrono altri pezzi di codice chiedete pure, ho cercato stringere un po'.
Help! :(