【发布时间】:2010-07-02 16:43:50
【问题描述】:
我正在尝试将全局函数声明为类的“朋友”:
namespace first
{
namespace second
{
namespace first
{
class Second
{
template <typename T> friend T ::first::FirstMethod();
};
}
}
}
当我在 Visual C++ 2008 下编译这段代码时,我得到:
error C3254: 'first::second::first::Second' : class contains explicit override 'FirstMethod' but does not derive from an interface that contains the function declaration
error C2838: 'FirstMethod' : illegal qualified name in member declaration
如果我改用template <typename T> friend T first::FirstMethod();,我会得到:
error C2039: 'FirstMethod' : is not a member of 'first::second::first'
声明友元函数的适当方式是什么?
【问题讨论】:
-
更一般地说,我认为嵌套相同的标识符(
first和first)是一个非常糟糕的主意,它不仅会混淆读者,还会混淆编译器。
标签: c++ visual-studio-2008 friend-function