所以我试图通过使用boost :: ptr_vector来摆脱我的std :: vector.现在我正在尝试从一个元素中删除元素,并删除已删除的元素.对我来说最明显的事情是:
class A
{ int m; };
boost::ptr_vector<A> vec;
A* a = new A;
vec.push_back(a);
vec.erase(a);
Run Code Online (Sandbox Code Playgroud)
但这甚至不会编译(请参阅下面的完整错误消息).我尝试了删除/删除习惯,就像我在std :: vector上所做的那样,但是boost :: ptr_vector的所有算法都与std :: vector中的算法略有不同.
所以我的问题:
编译错误:
1>------ Build started: Project: ptr_vector_test, Configuration: Debug Win32 ------
1>Compiling...
1>ptr_vector_test.cpp
1>c:\users\rvanhout\svn\trunk\thirdparty\boost\range\const_iterator.hpp(37) : error C2825: 'C': must be a class or namespace when followed by '::'
1> c:\users\rvanhout\svn\trunk\thirdparty\boost\mpl\eval_if.hpp(63) : see reference to class template instantiation 'boost::range_const_iterator<C>' being compiled
1> with
1> [
1> C=A *
1> ]
1> c:\users\rvanhout\svn\trunk\thirdparty\boost\range\iterator.hpp(63) …Run Code Online (Sandbox Code Playgroud) 我正在使用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();
Run Code Online (Sandbox Code Playgroud)
我试过了:
#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; …Run Code Online (Sandbox Code Playgroud) 我只想拥有一双新鲜的眼睛,以下代码是正确的:
对象trifoo中包含的指针(存储在ptr_vector中)是共享指针f,g,h.
另外,在trifoo的构造函数中shared_ptr副本的结果是什么; 这是'共享'shared_ptr的正确方法,确保参考计数增加等.我所有其他疑问我能够测试验证,但我不知道如何检查(正确).任何批评也是受欢迎的.
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/shared_ptr.hpp>
class foo {
int a, b;
public:
foo(int A, int B) : a(A), b(B) {}
};
typedef boost::shared_ptr<foo> foo_ptr;
class trifoo {
foo_ptr c, d, e;
public:
trifoo(const foo_ptr& C, const foo_ptr& D, const foo_ptr& E) : c(C), d(D), e(E) {}
};
int main() {
for (int i = 0; i < 5000000; i++) {
foo_ptr f(new foo(1,2));
foo_ptr g(new foo(2,3));
foo_ptr h(new foo(4,5));
boost::ptr_vector<trifoo> tris;
tris.push_back(new trifoo(f, g, h));
}
return …Run Code Online (Sandbox Code Playgroud) 我最近发现boost ptr_vector对管理堆分配对象的集合很有用.指针集合库非常好,但不幸的是,我被一件事所困扰.
我的代码的另一部分需要显式保存指向ptr_vector中我的一个对象的指针(由于特定原因它不能作为引用).但是,当您访问ptr_vector中的对象时,您会得到一个引用T&(即使您使用了ptr_vector.push_back(T*)
无论如何我可以从boost :: ptr_vector中获得一个普通指针吗?
我需要有一个std::vector的boost::ptr_vector秒.为了简化管理,我将boost :: ptr_vector包含在一个class(Zoo)中,并创建了一个std :: vector(allZoos).查看用于复制此内容的最小代码:
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/utility.hpp>
class Animal
{
public:
virtual char type() = 0;
};
class Cat : public Animal
{
public:
char type() { return 1; }
};
class Zoo
{
public:
boost::ptr_vector<Animal> animals;
};
int main()
{
std::vector<Zoo> allZoos;
Zoo ourCityZoo;
ourCityZoo.animals.push_back(new Cat());
//Uncommenting any of the lines below causes error:
//allZoos.push_back(ourCityZoo);
//allZoos.clear();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
声明allZoos是可以的,但调用其任何成员函数会导致编译器错误:(完整错误日志太长,未发布)
C2259: 'Animal' : cannot instantiate abstract class …Run Code Online (Sandbox Code Playgroud) 我正在使用ptr_vector来存储"形状".我试图用派生的形状类来填充它,比如"圆圈",每当我试图向下转换它们时,我都会变坏.
class Shape
{
public:
virtual ~Shape() {};
virtual void print() { std::cout << "shape" << std::endl; };
};
class Circle :
public Shape
{
public:
void print() { std::cout << "circle" << std::endl; };
};
int main()
{
boost::ptr_vector<Shape> shapes;
shapes.push_back(new Circle);
BOOST_FOREACH(Shape shape, shapes)
{
Circle& tempCircle = dynamic_cast<Circle&>(shape);
if(&tempCircle != NULL)
tempCircle.print();
}
system("PAUSE");
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习一些Boost图书馆.我目前正在做我认为未来的家庭作业项目(学期尚未开始).然而,这个问题不是关于作业问题,而是关于Boost.
码:
/* AuctionApplication.h */
class AuctionApplication : boost::noncopyable
{
private:
boost::ptr_vector<Auction> auctions_;
boost::ptr_vector<Bidder> bidders_;
boost::ptr_vector<Bid> bids_;
/* AuctionApplication.cpp */
Bid *AuctionApplication::GetLatestBid(const Auction *auction)
{
Bid *highestBid = 0;
BOOST_FOREACH(Bid *bid, bids_) // Error here!
if (bid->GetAuction()->GetName() == auction->GetName())
highestBid = bid;
Run Code Online (Sandbox Code Playgroud)
BOOST_FOREACH用于使用与上面完全相同的代码的法向量.自从我开始使用ptr_vectors以来,我得到了错误:
error C2440: '=' : cannot convert from 'Bid' to 'Bid *'
让我相信ptr_vector以某种方式模糊了foreach方法的指针.
如果我反而写
BOOST_FOREACH(Bid *bid, bids_)
Run Code Online (Sandbox Code Playgroud)
我得到了四种类型的错误
error C2819: type 'Bid' does not have an overloaded member 'operator ->'
这很糟糕,因为我知道出价是一个指针.
如何BOOST_FOREACH正确地迭代ptr_vectors?
试图找到使用ptr_vector存储,访问和释放对象,尤其是当存储对象从其他遗传(ptr_vector不应该有对象切片的任何问题)的最佳方式.但是当运行下面的程序时,令人惊讶的是派生类没有被破坏.谁知道为什么?
#include <boost/ptr_container/ptr_vector.hpp>
#include <iostream>
#include <boost/ptr_container/ptr_map.hpp>
#include <boost/foreach.hpp>
using namespace std;
class A
{
public:
int id;
A() {cout<<"Constructed A()"<<endl;}
A(int i):id(i) {cout<<"Constructed A"<<i<<endl;}
~A() {cout<<"* Destructed A"<<id<<endl;}
};
class B:public A
{
public:
int i;
B() {cout<<"Constructed B"<<endl;}
B(int ii):i(ii) {id=ii;cout<<"Constructed B"<<i<<endl;}
~B() {cout<<"* Destructed B"<<i<<endl;}
};
class zoo
{
boost::ptr_vector<A> the_animals;
public:
void addAnimal(A* a) {the_animals.push_back( a );}
void removeAnimal(int id) {the_animals.release(the_animals.begin()+id); }
void removeOwnership(int id) {the_animals.release(the_animals.begin()+id).release();}
};
int main()
{
zoo z;
z.addAnimal( new B(0) ); …Run Code Online (Sandbox Code Playgroud) 我试图使用ptr_vector来存储一些指针,但是我的main方法一出现就出错了.这是我的代码:
int main(void)
{
boost::ptr_vector<string> temp;
string s1 = "hi";
string s2 = "hello";
temp.push_back(&s1);
temp.push_back(&s2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误消息:
Critical error detected c0000374
Windows has triggered a breakpoint in Path_Tree.exe.
This may be due to a corruption of the heap, which indicates a bug in Path_Tree.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while Path_Tree.exe has focus.
The output window may have more diagnostic information.
The program '[7344] …Run Code Online (Sandbox Code Playgroud) 我有一个自己的对象的ptr_vector列表.像这样的东西:
boost::ptr_vector<SomeClass> *list;
list->push_back(new SomeClass()>;
...
BOOST_FOREACH(SomeClass *tempObj, list) // [x]
{
tempObj->...
}
>‘boost::ptr_vector<SomeClass>*’ is not a class, struct, or union type
Run Code Online (Sandbox Code Playgroud)