S
S
s1rGAY2021-09-05 15:35:06
C++ / C#
s1rGAY, 2021-09-05 15:35:06

How to solve the error: "non-static non-member reference must be relative to the given object"?

There is this class:

class BST {
    struct node {
        map<string, string> data;
        node* left = nullptr;
        node* right = nullptr;
    };
    node* root;
 //дальше разные методы
}

I need to overload the += operator so that it replaces the public class
method (the method is fully functional):
void insert(string eng, string rus) {
        root = insert(eng, rus, root);
//здесь insert - приватный метод класса BST
    }

When overloaded:
friend void operator +=(string eng_word, string rus_word) {
        root = insert(eng_word, rus_word, root);
    }

Gives the error described in the question and swears at root, insert, root, how to fix this error?
But at the same time, it does not swear if the code is written for one parameter, those without the FRIEND declaration:
void operator +=(string eng_word) {
        root = insert(eng_word, eng_word, root);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-09-05
@wataru

friend void operator +=(string eng_word, string rus_word) {

Here you are redefining += for two strings. How do you want to call += on your class with two string parameters? How do you imagine it in code?
And the error that the compiler complains about is that in this case the operator is not a member of your class. On which class instance do you want to call Insert? root in which instance to rewrite?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question