【发布时间】:2012-08-31 14:25:02
【问题描述】:
我想使用boost::intrusive_ptr 来引用我的类x::Y,所以我为release 和add_ref 函数添加了references 字段和友元声明,它们应该在命名空间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 文档中解释的所有事情,但似乎我做错了什么。问题出在哪里?
【问题讨论】:
-
为什么要侵入式引用计数?
shared_ptr这里有什么问题,因为您的课程最初没有被引用。 -
请坚持这个问题。这只是解释我的问题的一个最小示例。
标签: c++ boost namespaces smart-pointers forward-declaration