0

I have stringSize declared as an int in my private section already just change that to size_t?

That would be the best, using an int can cause problems, when dealing with large strings (*), that's exactly where size_t comes in, it's intended to hold such large lengths.

*: if you declare a variable like this: int var; , then it will be a signed integer, signed tells you that the integer may be negative as well, but where would you ever need a negative string size? The answer is: you'll never need that.
An addition is that, when dealing with very large strings, finally all bits of the integer variable will be set to 1 (including the high order bit), but the high-order bit (of a signed integer) just indicates whether the value will be interpreted as a positive or as a negative value.
If the high-order bit is 1, the (signed) integer will be interpreted as a negative number, and this can lead to serious bugs in your class.
(This is only the simple explanation, if you want the more correct explanation then I suggest you to Google on: two's complement)

0

Ya I understand that would probably be better to do but my directions say it has to be an int. Ugh this is getting frustrating now. I just need to figure out this segmentation fault. I do appreciate all the help you are giving very much by the way.

0

Ya I understand that would probably be better to do but my directions say it has to be an int. Ugh this is getting frustrating now. I just need to figure out this segmentation fault. I do appreciate all the help you are giving very much by the way.

Uhm, well, you're only allowed to use an int?
Is using an unsigned int allowed then ?
Otherwise you just stay with int.

0

Ya I guess i'll just have to stick with the int.

OK, Then you can move on to the next part of your assignment.
If you're having any problems, then just post them as a reply on this thread :)

0

I'm still getting segmentation fault from the for loop I believe.

0
ostream& operator<<(ostream& osObject, const MyString& rightOp)
        {
        int i;
        for (i = 0; i < rightOp.stringStorage[i]; i++)
                cout << rightOp.stringStorage[i];

        return osObject;
        }
0
ostream& operator<<(ostream& osObject, const MyString& rightOp)
        {
        int i;
        for (i = 0; i < rightOp.stringStorage[i]; i++)
                cout << rightOp.stringStorage[i];

        return osObject;
        }

Well, that's still not correct, change your for-loop to: for (i = 0; i < rightOp.[B]stringSize[/B]; i++)

0

That fixed it awesome! Get another segmentation fault a bit farther down though :(

Looks like this part of my assign3.cpp is where it came from.

cout << "\nTest " << test++ << ": String concatenation\n" << endl;

   const MyString s3("bye"), s4("world");

   cout << "s3: " << s3 << endl;
   cout << "s4: " << s4 << endl << endl;

   cout << "s1 + s4 is " << s1 + s4 << endl;
   cout << "\"good\" + s3 is " << "good" + s3 << endl;
   cout << "s4 + \"ly\" is " << s4 + "ly" << endl;
   cout << "s2 + \", \" + s4 is " << s2 + ", " + s4 << endl;

   cout << "\nTest " << test++ << ": Subscripting\n" << endl;

here are the overloaders i believe apply to it

MyString MyString::operator+(const MyString& rightOp) const
        {
        strcat(this->stringStorage, rightOp.stringStorage);
        return *this;
        }

        MyString MyString::operator+(const char* rightOp) const
        {
        strcat(this->stringStorage, rightOp);
        return *this;
        }

        MyString operator+(const char* leftOp, const MyString& rightOp)
        {
        MyString result;
        result.stringSize = strlen(leftOp) + rightOp.stringSize;
        new char[result.stringSize];
        return result;
        }

#

MyString MyString::operator+(const MyString& rightOp) const

The addition operator should be overloaded to take two MyStrings, concatenate their text together, and return a new MyString that contains the result.
#

MyString MyString::operator+(const char* rightOp) const

The addition operator should be overloaded to take a MyString object and a C-style string, concatenate their text together, and return a new MyString object that contains the result.
#

MyString operator+(const char* leftOp, const MyString& rightOp)

The addition operator should be overloaded to take a C-style string and a MyString object, concatenate their text together, and return a new MyString object that contains the result.

This operator function must be implemented as a standalone function. Make the function a friend of the MyString class.

0

Replace: strlen(leftOp) with leftOp.stringSize :)
(strlen only works for null-terminated strings)

Edit:: strcat(this->stringStorage, rightOp.stringStorage); and strcat(this->stringStorage, rightOp); are also wrong.

Same for strcat: strcat only works for null-terminated strings, you'll have to implement string concatenation from scratch.

Edit::
To what pointer do you assign this allocated memory? new char[result.stringSize];

0

That gives me this error though


MyString.cpp: In function âMyString operator+(const char*, const MyString&)â:
MyString.cpp:106: error: request for member âstringSizeâ in âleftOpâ, which is of non-class type âconst char*â

0

leftOp is a C style string (presumably, since it is a stand alone const char *) so it should be strlen(leftOp), however, you don't do any concatenation, you just return result which now has a stringSize, but no strringStorage value.

Edit: You also have to do something with memory that stringStorage originally had before you can assign it new memory.

0

should I store new char[result.stringSize] into stringStorage?

0

First you will need to declare dynamic memory to temporarily hold the information in stringStorage and another temproray holding variable to keep track of the old size of stringStorage. Then copy the information in stringStorage to the temporary holding variable and assign the size of the old stringStorage to the temporary holding variable. Then delete old memory from stringStorage and declare it with the new size, once you have determined the new size. Then use one loop to copy to the information in the temporary holding variable back into the new stringStorage and then use another loop to copy the rightOp into the new stringStorage. Then delete the memory for the temporary variable declared with new.

0

I have to head out to work soon so I will be trying this later. Any other suggestions? If so go ahead and post them because I can check this at work tonight I believe.

0

This is what I got so far, this dynamic allocation and overloading is so new to me that I don't get quite it yet so thanks for dealing with me.

MyString result;
        new char[stringStorage] = result;
        delete char[stringStorage];

how wrong is this? haha

0

>leftOp is a C style string
Oh yes, I skipped too quickly over that post :P
Sorry, strlen will do fine then, so don't remove it.

0

read over my last post when you can take more time. This:

MyString result;
new char[stringStorage] = result;

is wrong on several fronts. First, I don't think the new operator should ever appear on the left hand side of an assignment operator. Second, you don't want to store stringStorage in another MyString, you want to store it in another char array. So it would be something like:

//store old values in temporary variables
char * tempStringStorageValue = new char[stringSize];
int tempStringSize = stringSize;

//get new value for stringSize = size of one variable + size of the other
//declare new memory for stringStorage
stringStorage = new char[stringSize];

//now do the copy loops and deleting the memory used by tempStringStorageValue.

I'd suggest you go back and do the overloaded + operator for two MyString objects before you try to do the one for a MyString concatenated to a C style string and for a C style string concatenated to a MyString. You should be able to leverage the code you write by converting the C style string into a MyString and then concatenate 2 MyString variables by calling the version you just wrote.

This project will do several things for you. #1 it will give you experience with management of dynamic memory. #2 it will give you some idea of why the string class of the Standard Template Library is so useful---it does all these steps for you! #3 it will give you experience with writing overloaded operators. #4 it will give you experience with the friend operator. All in all, not a bad assignement.

0

I actually agree with this being a nice assignment. I feel that once I do figure everything out this will be a great reference I went through this same thing first learning functions in the spring and now its nothing to me. So far though no more luck with the code. I am going to attach what I have to this post. I need to read over the pointer information linked above still.

Attachments
/*********************************************************************
CSCI 241 Assignment 3

Programmer: your name
Logon ID:   your z-id
Date Due:   7/13/2009

Purpose: This driver program tests the MyString class.
*********************************************************************/

#include "MyString.h"
#include <iostream>

using namespace std;

int main()
   {
   int test = 1;

   cout << "Test " << test++ << ": Constructors and printing\n" << endl;

   const MyString  s1, s2("Hello");

   cout << "s1: " << s1 << endl;
   cout << "s2: " << s2 << endl;

   cout << "\nTest " << test++ << ": Size\n" << endl;

   cout << "s1 contains " << s1.size() << " characters\n";
   cout << "s2 contains " << s2.size() << " characters\n";

   cout << "\nTest " << test++ << ": String concatenation\n" << endl;

   const MyString s3("bye"), s4("world");

   cout << "s3: " << s3 << endl;
   cout << "s4: " << s4 << endl << endl;

   cout << "s1 + s4 is " << s1 + s4 << endl;
   cout << "\"good\" + s3 is " << "good" + s3 << endl;
   cout << "s4 + \"ly\" is " << s4 + "ly" << endl;
   cout << "s2 + \", \" + s4 is " << s2 + ", " + s4 << endl;
/*      
   cout << "\nTest " << test++ << ": Subscripting\n" << endl;

   const MyString s5("hot dog");
   MyString s6("cough");

   cout << "s5: " << s5 << endl;
   cout << "s6: " << s6 << endl;

   cout << "\ns5[0] = " << s5[0] << endl;
   cout << "s5[3] = " << s5[3] << endl;
   cout << "s5[6] = " << s5[6] << endl;

   cout << "\ns6[0] = " << s6[0] << endl;
   cout << "s6[1] = " << s6[1] << endl;

   s6[0] = 'l';
   s6[1] = 'a';

   cout << "\ns6: " << s6 << endl;

   cout << "\nTest " << test++ << ": Equality\n" << endl;

   const MyString s7("good"), s8("bye");

   cout << s7 << " and " << s7 << " are ";

   if (s8 == s3)
      cout << "equal" << endl;
   else
      cout << "not equal" << endl;

   cout << s7 << " and " << s8 << " are ";

   if (s7 == s8)
      cout << "equal" << endl;
   else
      cout << "not equal" << endl;

   cout << "\nTest " << test++ << ": Copy constructor\n" << endl;

   MyString s9 = s5;
   MyString* s10 = new MyString(s6);
   
   cout << "s9: " << s9 << endl;
   cout << "s10: " << *s10 << endl;

   cout << "\nTest " << test++ << ": Assignment operators\n" << endl;
   
   MyString s11;

   s9 = s4;
   *s10 = *s10;
   s11 = *s10 = s2;

   cout << "s9: " << s9 << endl;
   cout << "s10: " << *s10 << endl;
   cout << "s11: " << s11 << endl;

   s9 = "This is some new text";

   cout << "\ns9: " << s9 << endl;

   cout << "\nTest " << test++ << ": Destructor\n" << endl;

   delete s10;

   cout << "s9: " << s9 << endl;
   cout << "s11: " << s11 << endl;
*/
   return 0;
   }
/****************************************************************
   FILE:      MyString.cpp
   AUTHOR:    Justin R. Smith
   LOGON ID:  Z136340
   DUE DATE:  7/13/2009

   PURPOSE:   Contains method implications for the MyString class.
****************************************************************/

#include "MyString.h"
#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

	MyString::MyString()
	{
	stringStorage = 0;
	stringSize = NULL;
	}

	MyString::MyString(const char* s)
	{
	stringSize = strlen(s);			
	stringStorage = new char[stringSize+1];

	for(size_t i = 0; i < stringSize; i++)
		stringStorage[i] = s[i];
	}	

	MyString::MyString(const MyString& oldMyString)
	{	
	stringStorage = new char[oldMyString.stringSize];
	for(int i = 0; i < oldMyString.stringSize; i++)
		stringStorage[i] = oldMyString.stringStorage[i];
	stringSize = oldMyString.stringSize;
	}

	MyString::~MyString()
	{
	delete[] stringStorage;
	}	

	const MyString& MyString::operator=(const MyString& rightOp)
	{
	if(this != &rightOp)
		{
		delete[] stringStorage;
		stringStorage = new char[rightOp.stringSize];
		for(int i = 0; i < rightOp.stringSize; i++)
			stringStorage[i] = rightOp.stringStorage[i];
		stringSize = rightOp.stringSize;
		}
	return *this;
	}

	const MyString& MyString::operator=(const char* rightOp)
	{
	int i = 0;

	if (NULL != stringStorage)
	{
		delete[] stringStorage;
	}
	stringSize = strlen( rightOp );

	stringStorage = new char[stringSize];

	for( i = 0; i < stringSize; i++)
		stringStorage[i] = rightOp[i];

	stringStorage[i] = 0;	
	return *this;
	}

	ostream& operator<<(ostream& osObject, const MyString& rightOp)
	{
	int i;
	for (i = 0; i < rightOp.stringSize; i++)
		cout << rightOp.stringStorage[i];

	return osObject;
	}
	
	int MyString::size() const
	{
	return stringSize;
	}

	MyString MyString::operator+(const MyString& rightOp) const
	{
	strcat(this->stringStorage, rightOp.stringStorage);
	return *this;
	}

	MyString MyString::operator+(const char* rightOp) const
	{
	strcat(this->stringStorage, rightOp);
	return *this;
	}

	MyString operator+(const char* leftOp, const MyString& rightOp)
	{
        MyString result;
	result.stringSize = strlen(leftOp) + rightOp.stringSize;
	new char[result.stringSize];
	return result;		
	}

	char MyString::operator[](int ndx) const
	{
	return stringStorage[ndx];
	}

	char MyString::operator[](int ndx)
	{
	return stringStorage[ndx];
	}
	
	bool MyString::operator==(const MyString& rightOp) const		
	{
	if(strcmp(stringStorage, rightOp.stringStorage) == 0 && strlen(stringStorage) == strlen(rightOp.stringStorage))
		return true;
	else
		return false;
	}
/****************************************************************
   FILE:      MyString.h
   AUTHOR:    Justin Smith
   LOGON ID:  Z136340
   DUE DATE:  7/13/09

   PURPOSE:   does the declarion of the class MyString
****************************************************************/

#ifndef MYSTRING_H
#define MYSTRING_H
#include <iomanip>
#include <iostream>

using namespace std;
class MyString
	{
	friend ostream& operator<<(ostream&, const MyString&);
	friend MyString operator+(const char*, const MyString&);

	private:
		char* stringStorage;
		int stringSize;
	public:
		MyString();
		MyString(const char*);
		MyString(const MyString&);
		~MyString();
		const MyString& operator=(const MyString&);
		const MyString& operator=(const char*);
		int size() const;
		MyString operator+(const MyString&) const;
		MyString operator+(const char* rightOp) const;
		char operator[](int ndx) const;
		char operator[](int ndx);
		bool operator==(const MyString&) const;
	};

#endif /* MYSTRING_H */
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.