using std:vector array of boost::scoped_ptr

V

Vimal

Hi Folks,

I am trying to create an vector array of boost::scoped_ptr, and am
unable to complie a simple code...

I get the following error message on Visual studio 2005 (vc++ 8.0)
with boost 1.35

c:\program files\microsoft visual studio 8\vc\include\vector(1125) :
error C2248: 'boost::scoped_ptr<T>::scoped_ptr' : cannot access
private member declared in class 'boost::scoped_ptr<T>'
1> with
1> [
1> T=Test
1> ]
1> c:\boost\include\boost-1_35\boost\scoped_ptr.hpp(45) : see
declaration of 'boost::scoped_ptr<T>::scoped_ptr'
1> with
1> [
1> T=Test
1> ]
1> c:\program files\microsoft visual studio 8\vc\include\vector
(1117) : while compiling class template member function 'void
std::vector<_Ty>::_Insert_n(std::_Vector_iterator<_Ty,_Alloc>,__w64
unsigned int,const _Ty &)'
1> with
1> [
1> _Ty=boost::scoped_ptr<Test>,
1> _Alloc=std::allocator<boost::scoped_ptr<Test>>
1> ]
1> e:\tmp\myfirstmultithread\myfirstmultithread
\myfirstmultithread.cpp(131) : see reference to class template
instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=boost::scoped_ptr<Test>
1> ]


I am attaching the code

--- Start of code --
include <iostream>
#include <vector>
#include <boost/scoped_ptr.hpp>
class Test{
int x_axis;
int y_axis;
public:
Test():x_axis(0),y_axis(0){}
Test(int x,int y):x_axis(x), y_axis(y){}
int add(){
return x_axis+y_axis;
}
};

Test* createTestFactory();

int _tmain(int argc, _TCHAR* argv[])
{
/*Test t = new Test(1,2);
t.add();*/

boost::scoped_ptr<Test> t_scoped (new Test(1,3));

Test *t1 = createTestFactory();
int retval = t1->add();

boost::scoped_ptr<Test> t2(createTestFactory());

std::vector<boost::scoped_ptr<Test>> myvec;

myvec.push_back(t2);
return 0;
}

Test* createTestFactory()
{
return new Test();
}


-- End of code ---

I will be grateful if you can suggest a way to create a vector of
scoped pointers

Cheers
 
A

AnonMail2005

Hi Folks,

I am trying to create an vector array of boost::scoped_ptr, and am
unable to complie a simple code...

I'm not sure if this is your problem but if it's not, it will be:
A std::vector<T> needs to have T be copyable.

I see from the boost docs that scoped_ptr is not copyable so you
shouldn't do this.
Try using shared_ptr instead.

HTH
 
B

Bo Persson

Vimal said:
Hi Folks,

I am trying to create an vector array of boost::scoped_ptr, and am
unable to complie a simple code...

I get the following error message on Visual studio 2005 (vc++ 8.0)
with boost 1.35

c:\program files\microsoft visual studio 8\vc\include\vector(1125) :
error C2248: 'boost::scoped_ptr<T>::scoped_ptr' : cannot access
private member declared in class 'boost::scoped_ptr<T>'
1> with
1> [
1> T=Test
1> ]
1> c:\boost\include\boost-1_35\boost\scoped_ptr.hpp(45) : see
declaration of 'boost::scoped_ptr<T>::scoped_ptr'
1> with
1> [
1> T=Test
1> ]
1> c:\program files\microsoft visual studio
8\vc\include\vector (1117) : while compiling class template member
function 'void
std::vector<_Ty>::_Insert_n(std::_Vector_iterator<_Ty,_Alloc>,__w64
unsigned int,const _Ty &)' 1> with
1> [
1> _Ty=boost::scoped_ptr<Test>,
1> _Alloc=std::allocator<boost::scoped_ptr<Test>>
1> ]
1> e:\tmp\myfirstmultithread\myfirstmultithread
\myfirstmultithread.cpp(131) : see reference to class template
instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=boost::scoped_ptr<Test>
1> ]



I will be grateful if you can suggest a way to create a vector of
scoped pointers

You can't. The scoped_pointer is not supposed to be copied.

The first error message says that the (copy) constructor is private.


Bo Persson
 
S

SG

I am trying to create an vector array of boost::scoped_ptr, and am
unable to complie a simple code...

That is to be expected. boost::scoped_ptr doesn't satisfy
std::vector's "value type requirements". It's similar (even more
restrictive) to std::auto_ptr which also cannot be part of an STL
container.

Options:
- Do a redesign
- use some other smart pointer that works in a container
- wait for C++0x and use unique_ptr

Cheers,
SG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top