Search code examples
c++gccfriendvoid-pointers

Friend function returning (void*): cannot get implementation in .cpp file to work


Here is a shortcut of my code:

//myClass.h
namespace toto
{
  class myClass
  {
    friend void *myRoutine(void*);
    private:
      char* _name;
  }
}

//myClass.cpp
using namespace toto;
void *myRoutine(void* arg)
{
  myClass* foo = static_cast<myClass*>(arg);
  if ( NULL != foo )
  {
    cout << foo->_name;
  }
}

When compiling with GCC, I get an error "_name is private". I absolutly need that signature of function since it is used as a thread routine. And I'd like to avoid changing my attributes to public..

If somebody can help me, thanks !

Edit: In addition, myClass is defined in a namespace, and in the .cpp file I put a "using namespace ..." at the beginning.


Solution

  • Your friend declaration in myClass, declares a toto::myRoutine while your function definition defines a ::myRoutine. Define myRoutine in the same namespace as myClass to fix your problem.