C全球外部“C”的朋友不能达到名字空间的私人会员

C全球外部“C”的朋友不能达到名字空间的私人会员,第1张

概述请考虑代码: #include <iostream>using namespace std;extern "C"void foo( void );namespace A{ template< int No > class Bar { private: friend void ::foo( void ); 请考虑代码:
#include    <iostream>using namespace std;extern  "C"voID    foo( voID );namespace   A{    template< int No >    class   bar    {    private:        frIEnd  voID    ::foo( voID );        static voID private_func( int n );    };    template< int No >    voID    bar< No >::private_func( int n )    {        cout << "A:bar< " << No << ">::private_func( " << n << " )" << endl;    }}extern  "C"voID    foo( voID ){    A::bar< 0 >::private_func( 1 );}int main( ){    cout << " ---- " << endl;    foo( );}

G给出:

> g++ -Wall -o extern_c extern_c.cppextern_c.cpp: In function ‘voID foo()’:extern_c.cpp:20:7: error: ‘static voID A::bar<No>::private_func(int) [with int No = 0]’ is privateextern_c.cpp:29:31: error: within this context

如果我评论namspace A,它将编译并正确运行.

我失踪了什么

我看了相关的话题,但找不到符合我问题的东西.

> C++: namespace conflict between extern “C” and class member
> Why this friend function can’t access a private member of the class?

感谢人.

编辑:

我现在相信外部的“C”与这个问题无关.
请忽略它.

解决方法 我不知道这个解释,但是如果你把foo()放在一个命名空间中,它就可以工作.
#include    <iostream>using namespace std;namespace C{    extern  "C"    voID    foo( voID );}namespace   A{    template< int No >    class   bar    {    private:        frIEnd  voID    C::foo( voID );        static voID private_func( int n );    };    template< int No >    voID    bar< No >::private_func( int n )    {        cout << "A::bar< " << No << ">::private_func( " << n << " )" << endl;    }}namespace C{    extern  "C"    voID    foo( voID )    {        A::bar< 0 >::private_func( 1 );    }}int main( ){    cout << " ---- " << endl;    C::foo( );}

结果:

bbcaponi@bbcaponi frIEnds]$g++ -Wall namespace_frIEnd.cpp -o namespace_frIEnd[bbcaponi@bbcaponi frIEnds]$./namespace_frIEnd ----A::bar< 0>::private_func( 1 )
总结

以上是内存溢出为你收集整理的C全球外部“C”的朋友不能达到名字空间的私人会员全部内容,希望文章能够帮你解决C全球外部“C”的朋友不能达到名字空间的私人会员所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1248099.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存