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)

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.

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.

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

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 :)

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

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

Could you show me the current version?

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

        return osObject;
        }
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++)

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.

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];

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*â

I've edited my previous post, could you take a new look at it?

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.

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

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.

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.

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

>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.

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.

>All in all, not a bad assignement.
Yes!
IMO, It's a very nice assignment :)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.