delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/02/29/19:14:58

From: "Brian MacBride" <macbride AT ix DOT netcom DOT com>
Newsgroups: comp.os.msdos.djgpp
References: <1hknbsct3dea6ovjh70bf8c4tt4jamj1d2 AT 4ax DOT com>
Subject: Re: ANY help would be greatly appreciated...
Lines: 97
Organization: Signal Computing Service Ltd.
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2919.6600
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600
Message-ID: <wbYu4.18189$Qa5.94060@hnlnewsr2.hawaii.rr.com>
Date: Tue, 29 Feb 2000 22:59:40 GMT
NNTP-Posting-Host: 204.210.112.64
X-Complaints-To: abuse AT rr DOT com
X-Trace: hnlnewsr2.hawaii.rr.com 951865180 204.210.112.64 (Tue, 29 Feb 2000 12:59:40 HST)
NNTP-Posting-Date: Tue, 29 Feb 2000 12:59:40 HST
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"Tim" <opus AT wam DOT umd DOT edu> wrote in message
news:1hknbsct3dea6ovjh70bf8c4tt4jamj1d2 AT 4ax DOT com...
> I realize that this is alot to sort through, but here is the code for

    Snip...

     Probably not a good idea to use 'string' as a class name. Sooner or
later, you'll have ambiguity between your string class and the std::string
class. However, your problem is in your '+' operator. Try this instead...

> class string {
    // string &operator + (string &);
    string &operator += (const string &);
> };
>

/*
string &string::operator + (string &x) {
  if (x.length != 0) {
    int newlength = length + x.length;
    char *tempstring = new char[length + 1];
    int counter, counter2;

    for (counter = 0; counter < length; counter++) {
      tempstring[counter] = data[counter];
    }
    tempstring[counter] = '\0';
    cout << "This is from inside +: " << tempstring << endl;
    for (counter2 = 0; counter2 < x.length; counter2++) {
      tempstring[counter + counter2] = x.data[counter2];
    }
    cout << "This is from inside +: " << tempstring << endl;
    tempstring[counter + counter2] = '\0';
    delete[] data;
    length = newlength;
    data = tempstring;
  }
  return *this;
}
*/

string &string::operator += (const string &x) {
  if (x.length) {
    char *tmp = data, *p, *q = data;
    length += x.length;
    p = data = new char[length + 1];
    while (*q)
      *p++ = *q++;
    q = x.data;
    while (*q)
      *p++ = *q++;
    *p = 0;
    delete[] tmp;
  }
  return *this;
}

int main  () {

  string Test1 ("Hello there cruel world.");
  string Test2 ("Dude, how about that philosophy test!");

  cout << Test1 << endl;
  cout << Test2 << endl;

  Test1 += Test2;

  cout << Test1 << endl;
  Test1.stripBlanks ();
  cout << Test1 << endl;
  return 0;
}

/*
Yields...

Hello there cruel world.
Dude, how about that philosophy test!
Hello there cruel world.Dude, how about that philosophy test!
It's in stripBlanks.
Number of spaces wasn't equal to zero.
8
Hello there cruel world.Dude, how about that philosophy test!
It successfully copied over the array
Hellotherecruelworld.Dude,howaboutthatphilosophytest!
*/

Is that what you wanted??

> Thanks again!!!

Regards

Brian


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019