- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想使用 boost::intrusive_ptr
来引用我的类 x::Y
,所以我添加了一个 references
字段和友元声明对于 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 文档中解释的所有事情,但似乎我做错了什么。问题出在哪里?
最佳答案
错误是因为您在类定义中引用了 boost
命名空间,在该命名空间的任何声明之前。您可以通过在类定义之前声明 namespace boost
来修复它;您还需要声明函数,为此您还需要声明类和命名空间:
namespace x {class Y;}
namespace boost
{
void intrusive_ptr_add_ref(x::Y * p);
void intrusive_ptr_release(x::Y * p);
}
但是,最好不要将函数放在 boost
命名空间中,而是放在包含您的类的命名空间中(即 namespace x
)。然后 intrusive_ptr
将通过依赖于参数的名称查找找到正确的版本。这不需要在课前进行任何声明。
关于C++ intrusive_ptr 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12217372/
我已经阅读了很多关于 smart ptr 的内容,并决定在我自己的实现中使用 intrusive_ptr 进行引用计数。 也就是说,我现在不得不面对另一个问题,如何解决考虑到 weak_ptr 不能与
我想使用 boost::intrusive_ptr 来引用我的类 x::Y,所以我添加了一个 references 字段和友元声明对于 release 和 add_ref 函数,它们应该在命名空间 b
boost::intrusive_ptr (或自制版本)最简单的样子是这样的: template class intrusive_ptr { public: intrusive_ptr(T*
是否有可能找出要为使用 boost::intrusive_ptr 的库链接哪个库文件? 我尝试使用 boost bcp 工具,但这并没有给出编译 dylib boost::intrusive_ptr
我有 A 类,它使用 boost::intrusive_ptr 保存一些数据: #include "Data.h" class A { boost::intrusive_ptr data; }
我有一个基类,它为子类提供 intrusive_ptr_add_ref 和 intrusive_ptr_release 以与 boost::intrusive_ptr 一起使用。 有问题的代码在 Ma
我正在使用 boost::intrusive_ptr 来处理自动内存管理,但现在我想将它们与池化对象分配结合使用。 Boost Pool 是一个很好的起点,还是有另一种普遍接受的使用“智能指针”进行池
在我的代码中,当涉及到 intrusive_ptrs 时,我遵循两条规则: 按值传递原始指针意味着保证原始指针在该函数的生命周期内有效。 如果要在函数的生命周期之外存储和使用原始指针,则应将其存储在
我正在使用 boost::intrusive_ptr 作为我的引用计数智能指针。我正在使用这样的东西: http://www.codeproject.com/KB/stl/boostsmartptr.
在工作中,我们有一个基类,我们称它为 IntrusiveBase,它的作用类似于混合,允许将类存储在 boost:intrusive_ptr 中。也就是说,它为其子类提供引用计数并定义 intrusi
我读过 article about using boost::intrusive_ptr for managing COM objects .作者展示了一个包装类,它负责为通常的 COM 语义调整智能
具体来说,我需要声明(据我所知)intrusive_ptr_{add_ref,release} 作为我引用类的 friend : #include using boost::intrusive_pt
我有一个生产者-消费者安排来处理来自网络的事件。 Dispatcher 通过工作线程拥有的互斥锁保护队列为多个 EventHandler 线程提供工作。放入队列的事件对象使用boost::intrus
C++11 是否有与 boost::intrusive_ptr 等价的东西? 我的问题是我的 C++ 代码有一个 C 风格的界面。接口(interface)的两端都可以使用 C++,但出于兼容性原因,
我在 boost::intrusive_ptr 中包含一个 Locker 类型的小模板类,我想将其存储在 std::map 中: template bool LockerManager:: Ad
假设我有一个 list类: template class list { ... private: class node { ... private:
违规代码: template class SharedObject { public: typedef boost::intrusive_ptr Pointer; typedef boost
要求 我正在编写一个名为RCObject的类,它表示“引用计数对象”; RCObject类应该是抽象的,用作框架的基类(EC++3项目7); 应该禁止在堆栈上创建RCObject子类的实例(MEC++
我想一次准确地分配一个对象并将其推送到几个列表中。如何使用 boost::intrusive_ptr 执行此操作和 boost::intrusive::list ?或者我应该使用另一个容器和引用计数器
boost::intrusive 文档描述了如何 you can use smart pointers with intrusive containers但接着说你不能使用你最有可能使用的智能指针,“
我是一名优秀的程序员,十分优秀!