Original Post
I've got a string class that has never let me down before, but now I'm getting some errors when trying to do an = operation. It looks to me like it has something to do with the memory referencing since that's the error that is generated, a memory error. The copy constructor seems to take an array of characters just fine, but it seems that the problem is when I try to assign a String created from a character array to another String class.
Here is the code for the String class:
I know only that which I know, but I do not know what I know. [edited by - Daishi on January 25, 2004 2:08:16 PM]
#include <string.h>
class STRING
{
STRING(unsigned int);
char *DataString;
unsigned int StringLength;
public:
STRING();
STRING(const char *const);
STRING(const STRING &);
~STRING();
char &operator[](unsigned int offset);
char operator[](unsigned int offset) const;
STRING operator+(const STRING&);
void operator+=(const STRING&);
STRING &operator=(const STRING &);
STRING Left(unsigned int Length);
STRING Right(unsigned int Length);
STRING Mid(unsigned int Start, unsigned int Length);
unsigned int GetLength() const {return StringLength;}
const char *GetString() const {return DataString;}
};
// STRING class member definitions
STRING::STRING()
{
DataString = new char[1];
DataString[0] = '\0';
StringLength = 0;
}
STRING::STRING(unsigned int len)
{
DataString = new char[len+1];
for(unsigned int i = 0; i <= len; i++)
DataString[i] = '\0';
StringLength = len;
}
STRING::STRING(const char *const cString)
{
StringLength = strlen(cString);
DataString = new char[StringLength + 1];
for(unsigned int i = 0; i < StringLength; i++)
DataString[i] = cString[i];
DataString[StringLength] = '\0';
}
STRING::STRING(const STRING &rhs)
{
StringLength = rhs.GetLength();
DataString = new char[StringLength + 1];
for(unsigned int i = 0; i < StringLength; i++)
DataString[i] = rhs[i];
DataString[StringLength] = '\0';
}
STRING::~STRING()
{
delete [] DataString;
StringLength = 0;
}
STRING &STRING::operator=(const STRING &rhs)
{
if(this == &rhs)
return *this;
delete [] DataString;
StringLength = rhs.GetLength();
DataString = new char[StringLength + 1];
for(unsigned int i = 0; i < StringLength ; i++)
DataString[i] = rhs[i];
DataString[StringLength] = '\0';
return *this;
}
char &STRING::operator[](unsigned int offset)
{
if(offset > StringLength)
return DataString[StringLength - 1];
else
return DataString[offset];
}
char STRING::operator[](unsigned int offset) const
{
if(offset > StringLength)
return DataString[StringLength - 1];
else
return DataString[offset];
}
STRING STRING::operator+(const STRING &rhs)
{
unsigned int totalLen = StringLength + rhs.GetLength();
STRING temp(totalLen);
unsigned int i;
for(i = 0; i < StringLength; i++)
temp[i] = DataString[i];
for(unsigned int j = 0; j < rhs.GetLength(); j++, i++)
temp[i] = rhs[j];
temp[totalLen] = '\0';
return temp;
}
void STRING::operator+=(const STRING &rhs)
{
unsigned int rhsLen = rhs.GetLength();
unsigned int totalLen = StringLength + rhsLen;
STRING temp(totalLen);
unsigned int i;
for(i = 0; i < StringLength; i++)
temp[i] = DataString[i];
for(unsigned int j = 0; j< rhs.GetLength(); j++, i++)
temp[i] = rhs[i - StringLength];
temp[totalLen] = '\0';
*this = temp;
}
STRING STRING::Left(unsigned int Length)
{
STRING Temp(Length);
for(unsigned int i = 0; i < Length; i++)
Temp[i] = DataString[i];
return Temp;
}
STRING STRING::Right(unsigned int Length)
{
STRING Temp(Length);
unsigned int Start = StringLength - Length;
for(unsigned int i = 0; i <= Length; i++)
Temp[i] = DataString[Start + i];
return Temp;
}
STRING STRING::Mid(unsigned int Start, unsigned int Length)
{
STRING Temp(Length);
Start -= 1;
for(unsigned int i = 0; i <= Length; i++)
Temp[i] = DataString[Start + i];
return Temp;
}
And then the actual code that's using it.
STRING FileName; char TempString[20]; long TempStringLength = 0; // Game Directory // fin.read((char *) &TempStringLength, sizeof(TempStringLength)); fin.read((char *) &TempString, TempStringLength); TempString[TempStringLength] = '\0'; FileName = TempString; //////////////////// fin.close();The error generates on the FileName = TempString line, which then traverses to the operator= member function of the STRING class which then ultimate generates the error at delete [] DataString. Also, the data is being read from the file, the character array does contain the proper string. Any ideas? ... I'm out of gas on this one, I've been stuck on it for two days.
I know only that which I know, but I do not know what I know. [edited by - Daishi on January 25, 2004 2:08:16 PM]