views:

41

answers:

3

Hi all, I have redefined the >> operator as a friend function in a template class in the header. In it, i need to call another function called inputHelper that I have also defined in the header. (input helper is recursive)

the header file is as follows:

template< typename NODETYPE > class Forest
{
    /* (other friends) */
    friend void inputHelper(istream& file, int previousDepth,
        ForestNode<NODETYPE>& previousNode, ForestNode<NODETYPE> *nodeArray,
        int nodeCount)
    {
        /* (dostuff) */
        if(someconditional)
        {
            /* call inputHelper */
        }
    }

    friend istream& operator>>(istream& file, Forest<NODETYPE>& f1)
    {
        /* (dostuff) */
        /* (call inputHelper) */
    }
public:
    /* ... */
private:
    /* ... */
}

However, at compile, it says |140|error: 'inputHelper' was not declared in this scope|. Do you have to do something special because they are both defined as friend functions in the header? I kind of understand that inputHelper is outside of the scope of the class, but I'm not sure how to resolve this.

+1  A: 

The friend function is not a member function. In other words, it's scope is outside the scope of your class.By declaring it friend, you are giving it special priviliges to access protected members of the class Forest, but the manner is which you must access the member methods should use the object.MememberMethod() syntax.

In this instance, you need to call f1.inputHelper(...), rather than directly calling inputHelper(..). If you call inputHelper like this, I'd imagine it should compile normally.

Vatsan
A: 

In the code that you posted you are are not declaring two friend functions, but two methods of the Forest class, since you've written the function body into the class definition.

You should let the friend function prototypes into the class but rewrite them outside the Forest class definition.

sergiom
A: 
Dima