I am going through a bit of a crisis where I need to friend a template class that is typedefed, but I am getting loads of errors(too many to paste here). I have put the MRE here so you guys don't have to set up the files.
Here are the .h files for completeness-sake:
//Keyboard.h
#pragma once
namespace wm
{
namespace io
{
class Keyboard
{
//friend wm::MessageHandler;
private:
Keyboard(const Keyboard&) = delete;
void operator=(const Keyboard&) = delete;
Keyboard() = default;
public:
static Keyboard& Instance();
static void SomeServerFunc();
public:
static void SomeClientFunc();
};
}
}
//Mouse.h
#pragma once
namespace wm
{
namespace io
{
class Mouse
{
//friend wm::MessageHandler;
//friend class wm::MessageHandler; ??
private:
Mouse(const Mouse&) = delete;
void operator=(const Mouse&) = delete;
Mouse() = default;
public:
static Mouse& Instance();
static void SomeServerFunc();
public:
static void SomeClientFunc();
};
}
}
//MessageHandler.h
#pragma once
#include "Keyboard.h"
#include "Mouse.h"
namespace wm
{
template < class Keyboard, class Mouse >
class _MessageHandler
{
public:
static _MessageHandler& Instance();
_MessageHandler(const _MessageHandler&) = delete;
void operator=(const _MessageHandler&) = delete;
private:
_MessageHandler() = default;
static void SomeMsgHandelingFunc(); //calls SomeServerFunc from window and mouse
};
typedef _MessageHandler<io::Keyboard, io::Mouse> MessageHandler;
}
As it is, above code compiles and runs fine. But I want to make the SomeServerFunc
function in both keyboard and mouse classes private and make MessageHandler
a friend of Keyboard
and Mouse
. I have tried different things but nothing has worked so far.
I was doing friend class wm::MessageHandler;
but I got elaborated type refers to typedef error.
Then I found out after c 11, you can do friend wm::MessageHandler;
which gives loads of error. I also tried
friend wm::_MessageHandler<io::Keyboard, io::Mouse>
;
friend class wm::_MessageHandler<io::Keyboard, io::Mouse>
;
friend wm::_MessageHandler<Keyboard, Mouse>
;
friend class wm::_MessageHandler<Keyboard, Mouse>
;
No luck.
Would really appreciate any help.
CodePudding user response:
The _MessageHandler
is not a class but a template. Try that:
namespace wm
{
template<class Keyboard, class Mouse> class MessageHandler;
namespace io
{
class Keyboard
{
template<class Keyboard, class Mouse> friend class wm::MessageHandler;
...
};
}
}
CodePudding user response:
to make the SomeServerFunc function in both keyboard and mouse classes private and make MessageHandler a friend of Keyboard and Mouse.
I add class declaration to mouse.h and keyboard.h,to make the compiler happy.Here is the code:
#pragma once
namespace wm
{
template < class Keyboard, class Mouse >
class _MessageHandler;
namespace io
{
class Keyboard;
class Mouse
{
friend class _MessageHandler<io::Keyboard, io::Mouse>;
private:
Mouse(const Mouse&) = delete;
void operator=(const Mouse&) = delete;
Mouse() = default;
public:
static Mouse& Instance();
private:
static void SomeServerFunc();
public:
static void SomeClientFunc();
};
}
}
#pragma once
namespace wm
{
template < class Keyboard, class Mouse >
class _MessageHandler;
namespace io
{
class Mouse;
class Keyboard
{
friend class _MessageHandler<io::Keyboard, io::Mouse>;
private:
Keyboard(const Keyboard&) = delete;
void operator=(const Keyboard&) = delete;
Keyboard() = default;
public:
static Keyboard& Instance();
private:
static void SomeServerFunc();
public:
static void SomeClientFunc();
};
}
}