Original Post
I have two classes in different namespaces and I'm trying to declare a single function of one of them a friend of the other (I don't want the whole class to be a friend)
(irrelevant parts omitted)
If I declare the entire class a friend, so the befriending line becomes just 'friend class rendering::DX10Renderer;' it works fine, it just doesn't like the friend function. In the void draw() method of the DX10Renderer, I get an error saying that the variable m_renderable is inaccessible. I think it might be to do with the fact that when the friend function is declared in BasicEntity, nothing is known of the interior of DX10Renderer so it can't befriend the draw() function. I've tried #including the DX10Renderer header, but it doesn't help.
Any ideas?
(irrelevant parts omitted)
#ifndef BASICENTITY_H
#define BASICENTITY_H
namespace rendering
{
class DX10Renderer;
}
namespace entities
{
class BasicEntity
{
friend void rendering::DX10Renderer::draw();
private:
Renderable* m_renderable;
}
}
#endif#ifndef DX10RENDERER_H
#define DX10RENDERER_H
namespace rendering
{
class DX10Renderer
{
public:
void draw();
}
}
#endif
// in DX10Renderer.cpp file, just an example to demonstrate how I'm using it. The actual code doesn't look like this
namespace rendering
{
void DX10Renderer::draw()
{
entities::BasicEntity aBasicEntity = new entities::BasicEntity();
Renderable* rndbl = aBasicEntity->m_renderable; // Error: member "entities::BasicEntity::m_renderable" is inaccessible
}
}If I declare the entire class a friend, so the befriending line becomes just 'friend class rendering::DX10Renderer;' it works fine, it just doesn't like the friend function. In the void draw() method of the DX10Renderer, I get an error saying that the variable m_renderable is inaccessible. I think it might be to do with the fact that when the friend function is declared in BasicEntity, nothing is known of the interior of DX10Renderer so it can't befriend the draw() function. I've tried #including the DX10Renderer header, but it doesn't help.
Any ideas?