how to order the definitions and declaration of class and friend function?

Viewed 59

I have a function that is friend of a class. Therefore the function can access all class member. However, I am confused with the order of definition.

If the function comes before the class then how do could it even get the class member? if the class come before the function, then it cannot know the function.

How to solve such cross reference conflict?

So I have code

class BP_Interface
{
   friend void CommonUtility::HandleToFrame(Handle&);
private:
   Handle frame_in_model;
}

// followed by definition
namespace CommonUtility
{
    void HandleToFrame(Handle& eR)
    {
       BP_Interface().frame_in_model = eR;
       return;
    }
}


visual studio IDE shows error, and says frame_in_model cannot be accessed.

1 Answers

Declare CommonUtility::HandleToGraph before the definition of BP_Interface and define it later.

namespace CommonUtility
{
    void HandleToGraph(Handle& eR);
}

class BP_Interface
{
   friend void CommonUtility::HandleToFrame(Handle&);
private:
   Handle frame_in_model;
};

// followed by definition
namespace CommonUtility
{
    void HandleToGraph(Handle& eR)
    {
       BP_Interface().frame_in_model = eR;
       return;
    }
}
Related