【发布时间】:2011-10-12 20:56:27
【问题描述】:
请考虑代码:
#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.cpp
extern_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 private
extern_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?
谢谢大家。
编辑:
我现在确信extern "C" 与问题无关。
请忽略它。
【问题讨论】:
标签: c++ templates namespaces extern