- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 boost 1.37,我正在尝试使用 boost::ptr_vector,并转移它的所有权,以便我可以从函数中返回它。查看 boost 文档 ( http://www.boost.org/doc/libs/1_36_0/libs/ptr_container/doc/tutorial.html#new-functions )
std::auto_ptr< boost::ptr_deque<animal> > get_zoo()
{
boost::ptr_deque<animal> result;
...
return result.release(); // give up ownership
}
...
boost::ptr_deque<animal> animals = get_zoo();
我试过:
#include "iostream"
#include <boost/ptr_container/ptr_vector.hpp>
class Item
{
public:
int my_val;
Item() : my_val(0) { }
};
class MyClass
{
private:
boost::ptr_vector<Item> items_;
public:
MyClass()
{
for (int i = 0; i < 10; ++i)
items_.push_back(new Item);
}
std::auto_ptr<boost::ptr_vector<Item> > getData() { return items_.release(); }
};
int totalItems(boost::ptr_vector<Item> items)
{
int total = 0;
boost::ptr_vector<Item>::iterator it;
for (it = items.begin(); it != items.end(); ++it)
total += (*it).my_val;
return total;
}
int main(int argc, char **argv)
{
MyClass cls;
boost::ptr_vector<Item> items = cls.getData();
int total = totalItems(items);
fprintf(stdout, "I found %d items!\n", total);
return 0;
编译器错误:
In function ‘int main(int, char**)’:
error: conversion from ‘std::auto_ptr<boost::ptr_vector<Item, boost::heap_clone_allocator, std::allocator<void*> > >’ to non-scalar type ‘boost::ptr_vector<Item, boost::heap_clone_allocator, std::allocator<void*> >’ requested
这是 boost 文档中的一个小故障吗?我可以获取 auto_ptr 和取消引用以取回 ptr_vector 吗?
最佳答案
是的,这是一个文档错误。与 auto_ptr
(以及任何其他具有所有权转移语义的类型)一样,ptr_deque
上的转移构造函数是显式
,因此您不能使用 =
初始化形式。你必须做:
boost::ptr_vector<Item> items(cls.getData());
还有,只是教程错了;实际的类引用正确地显示了这一点。如果你看here ,你会看到声明:
explicit reversible_ptr_container( std::auto_ptr<reversible_ptr_container> r );
注意显式
。
关于c++ - 发布 boost::ptr_vector,不匹配文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2277898/
我正在尝试使用 ptr_vector 来存储一些指针,但是我的 main 方法一启动就出现错误。这是我的代码: int main(void) { boost::ptr_vector temp;
我目前正在使用 C++ 使用 OpenGL 进行较低级别的编码。我来自沉重的 objc 背景,所以我对内存管理有一些了解,但我似乎无法理解“boost”库如何管理 ptr_vector 等容器类型。
#include #include using namespace std; class Derived { public: int i; Derived() {c
是否有可能插入和/或删除元素会使现有元素的迭代器无效。 谢谢。 最佳答案 是的。 documentation对于 boost::ptr_vector状态: A ptr_vector is a poin
我正在尝试使用 boost 的指针容器,并对其应用 STL 算法。我写了一段代码来排序 ptr_vector其中Point是一个有成员的类int x, y .代码如下: #include #incl
我正在开发游戏引擎,在之前的一个问题中有人建议我开始使用 boost::ptr_vector 来维护指针列表。 基本思路是有几个State的,每个State有一个SceneGraph。每个状态都有几个
我正在尝试在当前项目中使用 boost::ptr_vector,想知道是否可以在 push_back() 方法中使用参数化构造函数,或者我是否需要使用默认构造函数? 作为次要问题,如果我定义它,它是否
我想随机分配给一个 boost::ptr_vector。使用普通的 STL vector 我会这样做: class A{ }; A* a1 = new A(); A* a2 = new A(); ve
所以现在我有以下内容: boost::ptr_vector cvect; ifstream cDbase("datafiles/customers.txt");
我找不到成员函数 boost::ptr_vector 有什么...... 我能找到的是http://www.boost.org/doc/libs/1_54_0/libs/ptr_container/d
我有一个 boost::ptr_vector,我想按排序顺序维护它。 我正在创建一堆包含几个简单字符串和值的 Customer 对象,然后将它们存储在 boost::ptr_vector 中,并且需要
我想做这样的事情: typedef X* X_Pointer; boost::ptr_vector myvec; X_Pointer x = new X(); myvec.push_back(x);
设置 class Base { public: Base(); virtual ~Base(); int getType(); protected: int type;
试图找到使用 ptr_vector 存储、访问和释放对象的最佳方法,尤其是当存储的对象是从其他对象继承时(ptr_vector 不应该有对象切片的任何问题)。但是当运行下面的程序时,令人惊讶的是派生类
我正在遍历树节点的子节点。子级存储在 ptr_vector 中,在整个迭代过程中的某个时刻我陷入无限递归,但我不明白为什么。 这里是无限递归发生的方法(这个方法只是用来打印树结构到cout): std
我在程序中有以下类(class)。 class Class1 { public: boost::ptr_vector fields; } class Class2 {
ptr_vector 是可复制构造和可复制赋值的。当不知道底层对象的具体类型时,它如何深度复制底层对象? 最佳答案 boost::ptr_vector 容器有一个可选的模板参数,CloneAlloca
我在 std::vector 上使用了 boost::ptr_vector,因为它将为我处理所有指针的删除。但是当我这样做时: ptr_vector functors; functors.resize
拜托,有人能告诉我如何从同一个 ptr_vector 中切换两个元素的位置吗?新的动态内存分配。也就是说,我想使用 std::vector 执行与下面相同的操作,但使用 boost::ptr_vect
如何在不删除的情况下从 boost::ptr_vector 中排除指针? =) 最佳答案 ptr_vector v; v.push_back(new A); A *temp=v.release(v.b
我是一名优秀的程序员,十分优秀!