Is there a way to access member-function from the friend class?
// foo.h
template<typename T>
class A
{
bool operator()(Item* item)
{
ObjectClass c = get_class_from_item(item); // compiler error
...
}
};
class B
{
...
template<typename T> friend class A;
...
ObjectClass get_class_from_item(Item* item);
};
If it matters I use gcc 4.5.2
If
get_class_from_itemis supposed to be some kind of Factory function, which it seems to be, you need to make it static. You get the error because your compiler is looking for a function namedget_class_from_itemin classA. It will never see the function inBbecause it is not in the scope and you don’t have an instance of classB. Here is some more explanation on Wikipedia: http://en.wikipedia.org/wiki/Factory_function.This should do it:
Also, is there a reason that
Aneeds to be a friend class ofB? Or did you just do that trying to getget_class_from_itemto work? DoesAneed to be able to access some private elements ofB? Think carefully about this, most of the time there are better ways of obtaining what you want instead of throwing away all encapsulation by usingfriend.[Edit] Removed lines from code example to strip it to the bare minimum.