1

I made a class which has a friend function and when I declare it there are no problems but, when I write its code it returns me the error:

"Out-of-line definition of 'change' does not match any declaration in 'MyClass '".

Here's the code

template <class T>
class MyClass {
private:
    T a;
    
public:
    MyClass(T);
    ~MyClass();
    friend void change(MyClass);
};

template <class T>
MyClass <T> :: MyClass(T value) {
    a = value;
}

template <class T>
MyClass <T> :: ~MyClass() {}

template <class T>
void MyClass <T> :: change(MyClass class) { //Out-of-line definition of 'change' does not match any declaration in 'MyClass <T>'
    a = class.a;
}
K0sm
  • 47
  • 8
  • Why do you want to use `friend` here? What problem do you try to solve? How do you plan to use `change`? – t.niese Dec 03 '20 at 14:53

1 Answers1

3

friend void change(MyClass); does not declare a member function of MyClass, it is an instruction for the compiler to grant the free function¹ void change(MyClass); access to private/protected members of MyClass.

The free function you grant access to MyClass would then have to look that way:

template <class S>
void change(MyClass<S> obj) {
    obj.a; // obj.a can be accessed by the free function
}

But the friend then has to be declared that way in the class:

template <class T>
class MyClass {
private:
    T a;
    
public:
    MyClass(T);
    ~MyClass();

    template <class S>
    friend void change(MyClass<S>);
};

But based on a = class.a in change I think you actually want to have this:

template <class T>
class MyClass {
private:
    T a;
    
public:
    MyClass(T);
    ~MyClass();

    void change(MyClass);
};


template <class T>
MyClass <T> :: MyClass(T value) {
    a = value;
}

template <class T>
MyClass <T> :: ~MyClass() {}

template <class T>
void MyClass <T>::change(MyClass class) {
    a = class.a;
}

A member function of MyClass can always access all members of any instance of MyClass.

1: What is the meaning of the term “free function” in C++?

t.niese
  • 39,256
  • 9
  • 74
  • 101
  • 1
    Unfortunately, the friend declaration does not declare a function template but a specific overload. – molbdnilo Dec 03 '20 at 14:46
  • @largest_prime_is_463035818 you could make a member function of a different class a firend. – t.niese Dec 03 '20 at 14:46
  • well ok, maybe I didnt express myself properly. Removed the comment. – 463035818_is_not_an_ai Dec 03 '20 at 14:47
  • @largest_prime_is_463035818 but yes i know what you mean. – t.niese Dec 03 '20 at 14:47
  • @t.niese I didn't doubt that ;) – 463035818_is_not_an_ai Dec 03 '20 at 14:49
  • Which free function? Are you saying that I don't need to be it a friend function? – K0sm Dec 03 '20 at 14:50
  • @K0sm you don't say in your question why you want to use `friend`, so it is hard to tell. In the current form, I only can tell you how fried is intended to be used. – t.niese Dec 03 '20 at 14:53
  • I want to have access to the private member of the passed object. – K0sm Dec 03 '20 at 14:55
  • @K0sm a "free function" is a function that is not a member of a class, in case thats the misunderstanding. In your code `friend void change(MyClass);` does not make `change` a member of the class, it is a free function – 463035818_is_not_an_ai Dec 03 '20 at 14:59
  • Thank you, my teacher has not explained it and I had to search online. Now I understand the error. Thank you so much for your time guys. – K0sm Dec 03 '20 at 15:02
  • @K0sm if you want to learn c++ I highly suggest reading a good book. I don't know your teacher, but sadly many of the teachers don't know c++ that well and you often learn bad habits and inaccurate things about c++. – t.niese Dec 03 '20 at 15:06