Overloading operator problem

I am having an error in my compiler whenever I try to run my overloading function from my namespace inside my class, here's the function along with the error. I can provide my main file and the class/function file as well.

1
2
3
4
5
6
MyString MyString::operator += (const MyString& rightOp)
{
    *this = *this + rightOp;

    return *this;
}


error


undefined reference to `cs_mystring::MyString::operator=(cs_mystring::MyString const&)'|
undefined reference to `cs_mystring::MyString::MyString(cs_mystring::MyString const&)'|
Last edited on
Are those linker errors?
Have you not defined those two functions:

1
2
MyString::operator=(MyString const&)
MyString::MyString(MyString const&)

this works fine for me...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

struct test {

    int x, y, z;

     test operator += (const test& other) {

        this->x = this->x + other.x;
        return *this;
   }

};



int main() {
    test x;
    test y;
    y.x = 77;
    x.x = 9;
    x.y = 8;
    x.z = 7;

    x += y;
    std::cout << x.x << "\n";  //result 86

}


i can't be 100% certain based on what you've posted as to what exactly the issue is but my best guess is that you're attempting to dereference this add something to it but you're not specifying a specific member.

edit, sry changed it to closer match what you were trying to do
Last edited on
You're right dutch, it was linker errors with the copying functions. For some reason my compiler was highlighting an error inside the += function and I thought it was something generally wrong in my += overloading function.
Topic archived. No new replies allowed.