0

Just started learning C++ recently and I'm attempting to make my own string class from scratch. I'm currently working on concatenating strings by overloading += and + operators. After reading this article, basic-rules-of-operator-overloading, I have come up with the following implementation;

String & String::operator+=(const String &o) 
{
    char * newBuffer = new char[this->size() + o.size() - 1];

    //copy over 'this' string to the new buffer
    int index = 0;
    while (this->at(index) != 0x0)
    {
        *(newBuffer + index) = this->at(index);
        index++;
    }

    //copy over the param string into the buffer with the offset 
    //of the length of the string that's allready in the buffer
    int secondIndex = 0;
    while (o.at(secondIndex) != 0x0)
    {
        *(newBuffer + index + secondIndex) = o.at(secondIndex);
        secondIndex++;
    }

    //include the trailing null
    *(newBuffer + index + secondIndex) = 0x0;

    //de-allocate the current string buffer and replace it with newBuffer
    delete[] this->s;
    this->s = newBuffer;
    this->n = index + secondIndex;

    return *this;
}

inline String operator+(String lhs, const String &rhs)
{
    lhs += rhs;
    return lhs;
}

However, the compiler will not recognise the + overload! It does work if I place the function in the main test file (where I am calling the method) but not if I place it in my String.cpp file where all my other methods are located.

Here is my String.h file if you need it;

#include <iostream>

class String
{
    public:
                String(const char * s);
                String(const String &o);

        int     size() const;
        char    at(int i) const;
        String  &operator+=(const String &o);

private:
        char *  s;
        int     n;
        //needs to be a friend function defined OUTSIDE of the class as when using
        //ostream << String you do not have access to the ostream so they can't be 
        //member operators
        friend std::ostream & operator<<(std::ostream &os, const String &o);

};

Thanks for any help! (also, anything you think I can improve on in regards to my implementation would be graciously received)

1 Answers1

-2

Well everyone already explained, so it should be as simple as just adding the forward declaration to the end of your .h file like this:

#include <iostream>

class String
{
    public:
                String(const char * s);
                String(const String &o);

        int     size() const;
        char    at(int i) const;
        String  &operator+=(const String &o);

private:
        char *  s;
        int     n;
        //needs to be a friend function defined OUTSIDE of the class as when using
        //ostream << String you do not have access to the ostream so they can't be 
        //member operators
        friend std::ostream & operator<<(std::ostream &os, const String &o);

};

//forward declaration
String operator+(String lhs, const String &rhs);

The forward declaration just tells the compiler to look for a function with that signature. When it doesn't find it in your current .cpp file it looks up on the other .cpp files. I hope this helps!

Felipe Centeno
  • 2,911
  • 1
  • 21
  • 39
  • Just one more thing: I believe that the forward declaration of an inline function is not different from a normal function, so the forward declaration should not include the "inline" keyword. – Felipe Centeno Aug 26 '17 at 14:55
  • To be precise, it's not the compiler "looking" for the function, that's the linker's job. – Banex Aug 26 '17 at 14:58
  • Ive just got this now; Undefined symbols for architecture x86_64: "operator+(String, String const&)", referenced from: _main in test-91adeb.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – ImpendingShroom Aug 26 '17 at 15:14
  • If I make the actual function in my .cpp file not inline, it works.. so thanks! – ImpendingShroom Aug 26 '17 at 16:04
  • you're welcome. Remember you can choose the answer if it help you resolved your question, and let me try adding the inline. I'll edit the post to show how to do it with inline. – Felipe Centeno Aug 26 '17 at 16:06
  • Just add the definition of the inline function inside the .h file. – Felipe Centeno Aug 26 '17 at 16:36