Here's my code:
I get this runtime error: "Null Pointer Assignmenw"Code:#include <iostream.h>#include <conio.h> #include <string.h> class string { char *str; int len; public: string(){} string(char *a) { len = strlen(a); str = new char[len+1]; strcpy(str, a); } string (string &a) { len = a.len; str = new char[len+1]; strcpy(str, a.str); } ~string() { delete str; } string operator+(string); void display() { cout << str; } }; int main() { string s1("Hello, "), s2("how are you?"); clrscr(); cout << "\n\nName: Sourabh Verma | Roll Number: 2K11-MRCE-CSE-116\n\n"; string s3 = s1 + s2; // Use Reference Here !!!!! return 0; } string string :: operator+(string a) { string temp; temp.len = a.len + len + 1; strcpy(temp.str, str); strcat(temp.str, a.str); return temp; }
Notice the 'w' at the end ... weird, I know !!
How can I do the assigning at line marked with comment with reference ?