Hi,
i am trying to use overloading operators with C-strings, and i keep crashing when i enter the +operator
Here is my header file:
my CPP File:Code:#ifndef __cDATE_H #define __cDATE_H #include <iostream.h> class cSTRING { private: int _nCmpValue, _num1, _num2; char *pszName, *pszName1; public: cSTRING(); cSTRING(char *pname, int returnVal); cSTRING(char *pszStr1); cSTRING(cSTRING &rhs); ~cSTRING(); char *getName() { return pszName; } int getVal() { return _nCmpValue; } void two(char*,int); void print() { cout << _num1 << endl << _num2 << endl; } cSTRING& operator=(const cSTRING& cStr); cSTRING operator+(const cSTRING& cStr); cSTRING& operator+=(const cSTRING& cStr); int operator==(const cSTRING& cStr); cSTRING& operator!=(const cSTRING& cStr); }; #endif
my main file:Code:#include "cDATE.h" #include <iostream.h> #include <string.h> cSTRING::cSTRING(char *name, int returnVal) { _nCmpValue = returnVal; pszName = new char[strlen(name) + 1]; //Allocates an character array object strcpy(pszName, name); cout << "cSTRING Default Constructor Called" << endl; } cSTRING::cSTRING(char *name) { pszName = 0; pszName = new char[strlen(name) + 1]; strcpy(pszName, name); cout << "Name copied" << endl; } cSTRING cSTRING::operator+(const cSTRING& rhs) { cout << "inside +" << endl; return cSTRING(strcat(pszName, rhs.pszName)); } cSTRING::cSTRING(cSTRING &rhs) { _nCmpValue = rhs.getVal(); pszName = new char[strlen(rhs.getName()) + 1]; strcpy(pszName,rhs.pszName); } cSTRING::~cSTRING() { if (pszName) delete[] pszName; cout << "Destructor called" << endl; }; cSTRING::cSTRING() { }
basically i declare to strings that are dynamically allocated, and then try to concatanate them, but it doesnt work :(Code:#include "cDATE.h" #include <iostream.h> int main(void) { char szStr[] = "Hello"; char szStr2[] = "SHello"; int returnVal = 3; cSTRING setName(szStr); cSTRING setName1(szStr2); cout << "Now adding...." << endl; cSTRING oAdd = setName + setName1; cout << endl << endl << endl; oAdd.print(); return (0); }
i am guessing that i am not passing them right....or that i have not declared to seperate strings.



LinkBack URL
About LinkBacks


