0

I'm new learning c++ How do I use friend with member functions from two classes contain with each other? I could not find a good answer through Google

Below is my code:

#ifndef FriendTest_hpp
#define FriendTest_hpp
class FriendVisitor;
class FriendTest{
    friend int FriendVisitor::getTestAdd();
private:
    int add=23;
    int  getAdd(){
        return add;
    }
public:
    void test(){
        printf("hello");
    }
    FriendTest()=default;
};

#ifndef FriendVisitor_hpp
#define FriendVisitor_hpp
#include <stdio.h>
class FriendTest;
class FriendVisitor{
    FriendTest* test;
public:
    FriendVisitor(){

    }
    int getTestAdd();
};

#endif /* FriendVisitor_hpp */

the IDE gives me the wrong error is :

incomplete type 'FriendVisitor named in nested name specifier'

What is the solution?

Jake Symons
  • 468
  • 2
  • 10
  • 19
JerryLin
  • 119
  • 1
  • 5
  • 1
    This should work: `friend class FriendVisitor` even with incomplete type. But, you may not make `friend int FriendVisitor::getTestAdd();` as long as `FriendVisitor` is an incomplete type. – Scheff's Cat Jul 24 '18 at 15:35
  • thx for answering my confusion – JerryLin Jul 24 '18 at 16:10
  • Your approach would work if `FriendTest.hpp` would `#include "FriendVisitor.hpp"`. (Due to this, `class FriendVisitor` would not be incomplete anymore.) But be careful concerning cyclic include dependencies - these can you drive crazy. – Scheff's Cat Jul 24 '18 at 16:15
  • ok,thx for your answering again~ – JerryLin Jul 25 '18 at 02:34

1 Answers1

3

Your problem is here:

class FriendVisitor;
class FriendTest{
    friend int FriendVisitor::getTestAdd();

At this point in the compilation, the FriendTest class knows about the existence of the FriendVisitor class, but not any of its members, as its declaration is not complete. If you reorder your code to fully declare FriendVisitor first, then its declaration is complete once you declare the friend function in FriendTest and it compiles:

#include <stdio.h>
class FriendTest; // Forward declaration
class FriendVisitor{
    FriendTest* test; // Only references the class, so only forward declaration needed
public:
    FriendVisitor(){

    }
    int getTestAdd();
};

class FriendTest{
    friend int FriendVisitor::getTestAdd();  // FriendVisitor is fully declared, friend function is legal
private:
    int add=23;
    int  getAdd(){
        return add;
    }
public:
    void test(){
        printf("hello");
    }
    FriendTest()=default;
};
rdowell
  • 729
  • 4
  • 15
  • hi,thx for your answer,try for your codes ,it work! but I still have some confusion ,u mean I need declare two classes in a .h instead of two above, is there any way declaring in two .h? – JerryLin Jul 24 '18 at 16:09
  • You can declare them in one header or in two separate headers, however you want, though good practice is to keep separate classes in separate headers. I only wrote the example as I did because your example code didn't indicate they were in different files – rdowell Jul 24 '18 at 17:03