Gil*_*ili 2 c++ visual-studio-2008 friend-function
我试图将全局函数声明为类的"朋友":
namespace first
{
namespace second
{
namespace first
{
class Second
{
template <typename T> friend T ::first::FirstMethod();
};
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我在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
Run Code Online (Sandbox Code Playgroud)
如果我使用,template <typename T> friend T first::FirstMethod();我得到:
error C2039: 'FirstMethod' : is not a member of 'first::second::first'
Run Code Online (Sandbox Code Playgroud)
宣布朋友功能的适当方式是什么?
您已经意外地完成了我的测验 - 序列T ::first:: ...被解释为单个名称.你需要在冒号之间加一些标记T.解决方案也在链接问题中提供.
请注意,在任何情况下,您首先必须在其各自的命名空间中声明由限定名称指定的函数.
编辑:语法问题有不同的解决方案
template <typename T> friend T (::first::FirstMethod)();
template <typename T> T friend ::first::FirstMethod();
Run Code Online (Sandbox Code Playgroud)
如果您经常需要引用外部命名空间并且遇到此语法的问题,则可以引入命名空间别名
namespace first
{
namespace outer_first = ::first;
class Second
{
template <typename T> friend T outer_first::FirstMethod();
};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
862 次 |
| 最近记录: |