I have an assignment that I need to create a custom C type string class in C++. I am having trouble getting this to work. Currently my code crashes with a run time error right at the start. I also know many of my function are wrong but I want to get the member functions sorted before I go on and fix the other functions. Bear in mind that all the function prototypes were given to us and I can not change them. I need to write the 'guts' so to speak.
What is wrong with my constructor for a start?
#include <iostream> #include "tstr.h" using namespace std; //Default constructor to initialize the string to null TStr::TStr() { strPtr = 0; strSize = 0; } //constructor; conversion from the char string TStr::TStr(const char *str) { int i=0; while (str[i] != '/0') { strPtr = new char [strlen(str)+1]; for (i=0; i <strSize;++i) { strPtr[i] = str[i]; } ++i; } strSize = i; } //Copy constructor TStr::TStr(const TStr&) { } //Destructor TStr::~TStr() { if (strPtr) { delete[] strPtr; } } //subscript operators-checks for range char& TStr::operator [] (int i) { assert (i >= 0 && i < strSize); return strPtr[i]; } const char& TStr::operator [] (int i) const { assert (i >= 0 && i < strSize); return strPtr[i]; } //overload the concatenation oprerator TStr TStr::operator += (const TStr& str) { //this->strPtr += str.strPtr; //this->strSize += str.strSize; return *this; } //overload the assignment operator const TStr& TStr::operator = (const TStr& str) { if (this != &str) { delete[] strPtr; strPtr = new char[strSize = str.strSize]; assert(strPtr); for (int i=0; i<strSize; ++i) { strPtr[i] = str.strPtr[i]; } } return *this; } //overload two relational operators as member functions bool TStr::operator == (const TStr& str) const { return (strPtr == str.strPtr && strSize == str.strSize); } bool TStr::operator < (const TStr& str) const { return (strPtr < str.strPtr && strSize < str.strSize); } //the length of the string int TStr::size() { return strSize; } Thanks for any replies/help! :)
EDIT 1: Okay the constructor is now working but I am still getting a runtime error and I'm 90% sure it is to do with my overloaded += operator. It looks fine though and compiles okay. What am I missing?
(Note: Only small changes have been made to the above code, but let me know if you want to see the whole lot.)
//overload the concatenation oprerator TStr TStr::operator += (const TStr& str) { for(int i = 0; i < strSize; ++i) { strPtr[i] += str.strPtr[i]; } return *this; } EDIT 2: Okay this what I have now. Compiles fine but doesn't actually add the two strings together with the += like it should. Anyone got any ideas?
//overload the concatenation oprerator TStr TStr::operator += (const TStr& str) { char *buffer = new char[strSize + str.strSize + 1]; strcpy(buffer, strPtr); strcat(buffer, str.strPtr); delete [] strPtr; strPtr = buffer; return *this; } //overload the assignment operator const TStr& TStr::operator = (const TStr& str) { if (this != &str) { delete[] strPtr; strPtr = new char[strSize = str.strSize]; assert(strPtr); for (int i=0; i<strSize; ++i) { strPtr[i] = str.strPtr[i]; } } return *this; }
if (strPtr)in your destructor.delete[]ing aNULLpointer is a nop and won't crash.assertbecause it's remove in release builds. Use exceptions.sizeconst.