-1

I am learning friend functions (C++) but I can not understand why this code does not work. I get this

error: "error C2027: use of undefined type 'second'". (line number 6)

It is just an example of course (useless). I am trying to use a member function of another class as friend (just that function). I find some example in the web. But in one old post here someone said that a member function of another class cannot be friend of a class.. Is this true?

#include<iostream>
using namespace std;
class second;

class test
{
    friend void second::fun(test &);
public:
    int j = 89;
private:
    int t = 12;
};

class second
{
public:
    void fun(test &b)
    {
        cout << "Try " << b.j << endl;
    }
    int a = 29;
private:
    int u = 10;
};

int main()
{
    test b;
    second one;
    one.fun(b);
    return 0;
}
user5507798
  • 57
  • 1
  • 8

4 Answers4

1

To access second::fun, a complete definition of second is required. You can fix this if you reverse the order in which you define these classes and forward-declare test instead. But again, b.j requires test to be defined, so you'll have to separate and postpone the definition of second::fun:

#include<iostream>
using namespace std;
class test;

class second
{
public:
    void fun(test &b); // test must be declared for using a reference to it
    int a = 29;
private:
    int u = 10;
};

class test
{
    friend void second::fun(test &); // second must be defined to access its member
public:
    int j = 89;
private:
    int t = 12;
};

void second::fun(test &b)
{
    cout << "Try " << b.j << endl;   // test must be defined to access its member
}

int main()
{
    test b;
    second one;
    one.fun(b);
    return 0;
}
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
0

Try the following:

class test;

class second
{
public:
    void fun(test &b);
    int a = 29;
private:
    int u = 10;
};

class test
{
    friend void second::fun(test &);
public:
    int j = 89;
private:
    int t = 12;
};

void second::fun(test &b)
{
    cout << "Try " << b.j << endl;
}
101010
  • 41,839
  • 11
  • 94
  • 168
0

You have a couple issues with you code. For

friend void second::fun(test &);

To work the compiler must know what second is. Since it is an incomplete type you will get a compiler error. In order to fix that you need declare second before test. Doing that brings up another issue as

second::fun(test &b)

uses test. To fix this we would forward declare test and then declare second. After changing that you need to move the actual function definition out of second and have it after test.

#include<iostream>
using namespace std;
class test;
class second
{
public:
    void fun(test &b);
    int a = 29;
private:
    int u = 10;
};
class test
{
    friend void second::fun(test &);
public:
    int j = 89;
private:
    int t = 12;
};

void second::fun(test &b)
{
    cout << "Try " << b.j << endl;
}

int main()
{
    test b;
    second one;
    one.fun(b);
    return 0;
}

Live Example

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

When the compiler reads this line

friend void second::fun(test &);

it does not know whether the class second indeed has data member fun. To be sure that this line is correct the compiler needs the definition of the class second.

On the other hand, the definition of the function fun must know that class test has data member j.

To resolve the collision you can write the following way

#include<iostream>
using namespace std;

class test;

class second
{
public:
    void fun(test &b); // class test is already declared above
    int a = 29;
private:
    int u = 10;
};

class test
{
    friend void second::fun(test &); //class second is already defined above
public:
    int j = 89;
private:
    int t = 12;
};

void second::fun(test &b)
{
    cout << "Try " << b.j << endl; // class test is already defined above
}

int main()
{
    test b;
    second one;
    one.fun(b);
    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I tried but these are the errors in this case: Error 1 error C2061: syntax error : identifier 'test', Error 2 error C2245: non-existent member function 'second::fun' specified as friend (member function signature does not match any overload) , Error 3 error C2511: 'void second::fun(test &)' : overloaded member function not found in 'second', Error 4 error C2660: 'second::fun' : function does not take 1 arguments – user5507798 Dec 10 '15 at 17:07
  • @user5507798 See my updated code. You need to declare the class test before the class second – Vlad from Moscow Dec 10 '15 at 17:08