UPDATE: Allocated memory for str1's new data. Still memory error.
I'm trying to rewrite the += method for a string class I created.
Class mystring{
public:
friend void operator+=(mystring& str1, const mystring& str2){
mystring temp;
delete[] temp.data;
temp.length = str1.length + str2.length;
temp.data = new char[temp.length + 1];
strcpy(temp.data, str1.data);
strcat(temp.data, str2.data);
delete[] str1.data;
str1.length = temp.length;
strcpy(str1.data, temp.data);
}
private:
char *data;
int length;
}
Then in the main class:
mystring str1("hi");
mystring str2("matt");
str1 += str2;
cout << str1 << endl;
This function is working as it should be, but I am getting memory errors all over when I run valgrind. I am unable to figure out why that is. If anyone could give me any tips that would be awesome.
Thanks