Good night,
The program I'm developing does compile correctly, but when I perform some tests it crashes... I've searched for this error in a dozen pages but I couldn't find the answer to my problems :|
So the code is this (it's a little bit extensive but I think it's better this way to give all the info possible to debug it):
Code:
#include "cstring.cpp"
#include<cassert>
#include<iostream>
using namespace std;
template<class T> class Vector;
template<class T>
ostream& operator<<(ostream&, const Vector<T>&);
template <class T>
class Vector
{
friend ostream& operator<< <>(ostream&, const Vector<T>&);
private:
T* data;
unsigned len;
public:
Vector(unsigned = 10);
Vector(const Vector<T>&);
virtual ~Vector(void);
Vector<T>& operator =(const Vector<T>&);
bool operator==(const Vector<T>&);
T& operator [](unsigned);
unsigned getLength(void) {return len;}
T* getData(void) {return data;}
};
template <class T>
ostream& operator<<(ostream& os, const Vector<T>& v)
{
unsigned i;
for(i=0; i<v.len; i++)
os << v.data[i];
return os;
}
template <class T>
Vector<T>::Vector(unsigned size)
{
len=size;
data = new T[len];
}
template <class T>
Vector<T>::Vector(const Vector<T>& v)
{
unsigned i;
len = v.len;
data = new T[len];
for(i=0; i<len; i++)
data[i]=v.data[i];
}
template <class T>
Vector<T>::~Vector()
{
cout << "Deleting data..." << endl;
delete [] data;
}
template <class T>
Vector<T>& Vector<T>::operator=(const Vector<T>& v)
{
unsigned i;
len = v.len;
delete[] data;
data = new T[len];
for(i=0; i<len; i++)
data[i]=v.data[i];
return *this;
}
template <class T>
bool Vector<T>::operator==(const Vector<T>& v)
{
unsigned i;
if(len!=v.len)
return false;
else
{
for(i=0; i<len; i++)
{
if(data[i]!=v.data[i])
return false;
}
return true;
}
}
template <class T>
T& Vector<T>::operator [](unsigned n)
{
unsigned i;
assert(n>=0);
if(n>=len) // vector dinâmico
{
T* newData = new T[n+1];
for(i=0; i<len; i++)
newData[i]=data[i];
delete [] data;
data = newData;
len=n+1;
}
return data[n];
}
template<class P, class Q>
class Pair
{
P p;
Q q;
public:
Pair(const P& _p = P(), const Q& _q = Q()): p(_p), q(_q) {}
P& objectP() {return p;}
Q& objectQ() {return q;}
};
// herança
template<class KeyType, class ValueType>
class AssociativeArrayInheritance : public Vector<Pair<KeyType, ValueType> >
{
public:
AssociativeArrayInheritance(unsigned size=0):Vector<Pair<KeyType, ValueType> >(size){}
ValueType& operator [](const KeyType);
};
template<class KeyType, class ValueType>
ValueType& AssociativeArrayInheritance<KeyType, ValueType>::operator [](const KeyType key)
{
unsigned i;
Pair<KeyType, ValueType> pair(key);
for(i=0; i<Vector<Pair<KeyType,ValueType> >::getLength(); i++)
{
pair = Vector<Pair<KeyType,ValueType> >::getData[i];
if(pair.objectP() == key)
return pair.objectQ();
}
Vector<Pair<KeyType,ValueType> >::getData[Vector<Pair<KeyType,ValueType> >::getLength()+1]=pair;
return Vector<Pair<KeyType,ValueType> >::getData[Vector<Pair<KeyType,ValueType> >::getLength()-1].objectQ();
}
// composição
template <class KeyType, class ValueType>
class AssociativeArrayComposition
{
private:
Vector<Pair<KeyType, ValueType> > pairs;
unsigned len;
public:
AssociativeArrayComposition(unsigned size=0):pairs(size)
{
len = size;
}
virtual ~AssociativeArrayComposition(void);
ValueType& operator [](const KeyType);
};
template <class KeyType, class ValueType>
AssociativeArrayComposition <KeyType, ValueType>::~AssociativeArrayComposition()
{
delete [] &pairs;
}
template<class KeyType, class ValueType>
ValueType& AssociativeArrayComposition<KeyType, ValueType>::operator [](const KeyType key)
{
Pair<KeyType, ValueType> pair(key);
for(unsigned i=0; i < len; i++)
{
pair = pairs[i];
if(pair.objectP() == key)
return pair.objectQ();
}
pairs[len]=pair;
len++;
return pairs[len-1].objectQ();
}
The errors occur in the lines that are bold (in class 'AssociativeArrayInheritance').
The tests I made that originate these errors is:
AssociativeArrayInheritance<String, int> table;
String key1 (3, 'a'); // creates "aaa" string
table[key1] = 15; // error is here
Again I apologize for the lengthy code but I think is a necessary mean :x
Anybody has an idea? I think I have posted all the info needed, if not please say to me.
Thanks in advance,
João