I'm getting a segmentation fault and I have no idea how to debug it! It occurs after the creation of the MyString array i.e. the array created without any issue.
void ConcatTest()
{
cout << "\n----- Testing concatentation on MyStrings\n";
const MyString s[] =
{MyString("outrageous"), MyString("milk"), MyString(""),
MyString("cow"), MyString("bell")};
for (int i = 0; i < 4; i++) {
cout << s[i] << " + " << s[i+1] << " = " << s[i] + s[i+1] << endl;
}
}
So I think it may be something in how I overloaded the + operator here:
MyString operator+(MyString str1, MyString str2)
{
MyString resultStr = MyString();
delete [] resultStr.pString;
resultStr.pString = new char[strlen(str1.pString) + strlen(str2.pString) + 1];
MyString temp = MyString();
delete [] temp.pString;
temp.pString = new char[strlen(str1.pString) + 1];
strcpy(temp.pString, str1.pString);
delete [] str1.pString;
str1.pString = new char[strlen(str1.pString) + strlen(str2.pString) + 1];
strcpy(str1.pString, temp.pString);
strcat(str1.pString, str2.pString);
strcpy(resultStr.pString, str1.pString);
return resultStr;
}
Any kind of help or advice would be appreciated!