我想boost::intrusive_ptr
用于引用我的 class x::Y
,所以我为and函数添加了一个references
字段和朋友声明,它们应该在 namespace 中定义。然后,我编写这些函数。像这样:release
add_ref
boost
namespace x{
class Y{
long references;
friend void boost::intrusive_ptr_add_ref(x::Y * p);
friend void boost::intrusive_ptr_release(x::Y * p);
};
}
namespace boost
{
void intrusive_ptr_add_ref(x::Y * p)
{
++(p->references);
}
void intrusive_ptr_release(x::Y * p)
{
if (--(p->references) == 0)
delete p;
}
}
代码无法编译,我收到以下错误:
test/test6.cpp:8:18: error: ‘boost’ has not been declared
test/test6.cpp:9:18: error: ‘boost’ has not been declared
test/test6.cpp: In function ‘void boost::intrusive_ptr_add_ref(x::Y*)’:
test/test6.cpp:7:11: error: ‘long int x::Y::references’ is private
test/test6.cpp:17:9: error: within this context
test/test6.cpp: In function ‘void boost::intrusive_ptr_release(x::Y*)’:
test/test6.cpp:7:11: error: ‘long int x::Y::references’ is private
test/test6.cpp:22:13: error: within this context
我以为我做了 boost 文档中解释的所有事情,但似乎我做错了什么。哪里有问题?