1

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
  • Please post code that actually reproduces the problem (the pseudo-code you posted probably wouldn't compile for a number of reasons unrelated to your problem) and tell use where exactly do you get the error...
    – sth
    Oct 31, 2010 at 0:26

3 Answers 3

1

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.

0

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.

0

You are confusing two concepts here: a friend function and a member function. A friend function is defined outside the class definition. What is inside the class is only the friend declaration, which simply states that such and such non-member function defined elsewhere has access to the class' private members.

By including the body of inputHelper() and operator>> together with their friend declarations you have made it ambiguous whether they are member functions or friends. Furthermore, since these functions are defined entirely inside the class, they do not exist outside the class, which is why the compiler gives you an error when you try to use them.

Actually this is even more interesting. You are getting away with operator>> being a member function, because the syntax for using it is the same whether it is a member or not. However, when you call inputHelper() as though it is a non-member, then the compiler gives you an error.

The way to fix this is to make it clear to the compiler who is a friend and who is a member. If you want operator>> and inputHelper() to be friend functions, then you must only leave the friend declarations inside the class and put their definitions, i. e. their bodies outside the class.


template class Forest
{
    /* (other friends) */

    friend void inputHelper(istream& file, int previousDepth,
        ForestNode& previousNode, ForestNode *nodeArray,
        int nodeCount); 

    friend istream& operator>>(istream& file, Forest& f1);
public:
    /* ... */
private:
    /* ... */
};

void inputHelper(istream& file, int previousDepth,
        ForestNode& previousNode, ForestNode *nodeArray,
        int nodeCount)
{
        /* (dostuff) */
        if(someconditional)
        {
            /* call inputHelper */
        }
}

istream& operator>>(istream& file, Forest& f1)
{
        /* (dostuff) */
        /* (call inputHelper) */
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.