1

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.

Ricola3D
  • 2,402
  • 17
  • 16

4 Answers4

2

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.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
1

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

Don't do that. In the .cpp file, you must define the function inside the namespace, like this

namespace toto
{
  void* myRoutine(void* arg) { ...}
}

or like this

void* toto::myRoutine(void* arg)
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

myRoutine() should be written inside the namespace, either by

namespace toto
{
// write the function here
}

or

void *toto::myRoutine( //....
Rahul Pandey
  • 435
  • 1
  • 3
  • 13
-1

You can use get method in your class like ,

char* getname()
{
   return _name;
}

Than call foo->getname().

Hitesh Vaghani
  • 1,342
  • 12
  • 31
  • The example I gave you is a very shortcut. This is included in a very huge framework and I would like to avoid any possible public access that other developers could make a wrong use of ^^ Indeed some attributes are aggregated pointers, and thus I need a not-constant pointer to them in my routine. But I don't want anybodyelse to change the pointed object. – Ricola3D Jul 16 '13 at 09:48
  • This is the standard method of accessing private members of the class – Hitesh Vaghani Jul 16 '13 at 09:49
  • I know, it's the standard when you don't need specific privileges for some class or function. Otherwise you need the "friend" keyword – Ricola3D Jul 16 '13 at 09:51
  • Tried it, no difference. In addition, after reading it (http://stackoverflow.com/questions/6407691/friend-declaration-in-c-difference-between-public-and-private), It results the friend function/class is really not declared as a member, thus it doesn't take in consideration any private/public/protected key word just before its declaration... Maybe an issue of namespace ? The implementation is considered as a new function and not the implementation of my friend declared function ? – Ricola3D Jul 16 '13 at 09:55
  • In any case, this does not answer the question. – juanchopanza Jul 16 '13 at 10:03