0

So I'm working on a homework assignment where, after having written our own versions of the std::string class with some basic functions using a dynamic array of chars, we're supposed to translate that to a linked list. Everything was functional (though likely not as efficient as possible), but I've hit a snag. The lowest += overload function, where you'd just be adding a single char to the end of a string object, seems to work fine when I test it. The second one, where you'd be adding an array of chars to a string object, does not. Trying to use it causes a runtime error. Since the highest += overload function relies on the second one, that also isn't working.

I think the problem is related to the line:

headPtr += addend[index];

But I'm not sure what to use in place of headPtr if that is in fact the issue. Here's the code:

HEADER:

#ifndef STRING2_H
#define STRING2_H
#include <cstring>

namespace string2
{
class string
{
private:
    struct stringList
    {
        char character;
        stringList* link;
    };

    stringList* headPtr;

public:
    // CONSTRUCTORS AND DESTRUCTOR
    string() { headPtr = NULL; };
    string(const stringList* sourcePtr);
    ~string();

    // CONSTANT MEMBER FUNCTIONS
    char getChar(const size_t position) const;
    size_t length() const;
    char operator [ ] (size_t position) const;

    // MODIFICATION MEMBER FUNCTIONS
    void operator += (const string& addend);
    void operator += (const char addend[]);
    void operator += (char addend);
};
}
#endif

RELEVANT .CPP FUNCTION DEFINITIONS:

void string::operator += (const string& addend)
{
    for (int i = 0; i < addend.length(); i++)
        headPtr += addend[i];
}

void string::operator += (const char addend[])
{
    if (addend[0] == NULL)
        return;

    for (int index = 0; index < (sizeof(addend) / sizeof(addend[0])); index++)
        headPtr += addend[index];
}

void string::operator += (char addend)
{
    stringList *indexPtr = headPtr;

    if (headPtr == NULL)
    {
        headPtr = new stringList;
        headPtr->character = addend;
        headPtr->link = NULL;
        return;
    }

    while (indexPtr->link != NULL)
        indexPtr = indexPtr->link;

    indexPtr->link = new stringList;
    indexPtr->link->character = addend;
    indexPtr->link->link = NULL;
}

Help is appreciated!

Loggins
  • 11
  • 1
  • Possible duplicate of [Finding length of array inside a function](https://stackoverflow.com/questions/17590226/finding-length-of-array-inside-a-function) – phuclv Mar 25 '18 at 07:32
  • 1
    `index < (sizeof(addend) / sizeof(addend[0]))` this isn't gonna work because arrays will decay into pointers inside functions – phuclv Mar 25 '18 at 07:32
  • 1
    If you want to call through to another `+=` operator you can call it explicitly to avoid confusion. Try `this->operator+=(addend[i]);` instead of `headPtr += addend[i]` (the latter is completely wrong, as all you're doing is incrementing the pointer). – Jonathan Potter Mar 25 '18 at 07:33

1 Answers1

0

You right. The problem is headPtr += addend[index]; but nor because of operators overload, but because of memory violation. This will increment the headPtr pointer as many bytes ad the ASCII value of addend[index] so next time you will call the 'operator += (char addend)' it will write the memory somewhere "out of space".

I can see two issues:

  1. You can simplify the iteration over the 'addend' variable (see sample below).
  2. You are trying to add 'char' to the pointer headPtr. I think what you wish to do is to do exactly the same as in the operator +=(char a). So why not call it?

If you change '+=(const char[])' to this form, this should help:

void string::operator += (const char v[])
{
    for (int i = 0; v[i] != '\0'; i++) {
        (*this) += v[i]; // call to: void string::operator += (char addend)
    }
}

You can even turn in into such hacky form:

for (; *v != '\0'; v++)  (*this) += *v; 

or this:

for (; *v != '\0'; (*this) += *v, v++);

But I do not recommend it. It's too tricky and you can use it just like any exercise to understand the C part of C++.

I think you have the same issue with void string::operator += (const string& addend).

Vicctor
  • 803
  • 6
  • 13