473,385 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

auto_ptr vector discards qualifier

Hi all,

I'm trying to create a vector of pointers to a class:

For example:

class CMb ...

std::vector< CMb* > myvect(100);

myvect[10] = new CMb();

But this doesn't work. g++ just spews out a whole load of uninituitive
errors around the assignment operation.

So, have just read a post on this newsgroup from a few years ago, I'm
trying to use an auto_ptr instead.

so,

std::vector< std::auto_ptr<CMb> > myvect(100);
myvect[10] = new CMb();

But, this time the compiler complains that passing const
std::auto_ptr<CMb> discards a qualifier.

How to I fix this?

Thanks,

Stephen
Jul 22 '05 #1
2 2325
"stephen henry" <st******@yahoo.com> wrote in message
news:84**************************@posting.google.c om...
I'm trying to create a vector of pointers to a class:

For example:

class CMb ...

std::vector< CMb* > myvect(100);

myvect[10] = new CMb();

But this doesn't work. g++ just spews out a whole load of uninituitive
errors around the assignment operation.
The following code sample compiles without problem:
#include <vector>
class CMb {};

int main()
{
std::vector< CMb* > myvect(100);
myvect[10] = new CMb;
}

What was the error you were encountering?
So, have just read a post on this newsgroup from a few years ago, I'm
trying to use an auto_ptr instead.

so,

std::vector< std::auto_ptr<CMb> > myvect(100);
myvect[10] = new CMb();

But, this time the compiler complains that passing const
std::auto_ptr<CMb> discards a qualifier.


It is illegal to use a container of std::auto_ptr
(because instances of auto_ptr cannot properly be copied).
You could use another smart pointer instead, however,
such as boost::shared_ptr (from www.boost.org), which
will be adopted in the next revision of the standard.
hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #2

"stephen henry" <st******@yahoo.com> schrieb im Newsbeitrag
news:84**************************@posting.google.c om...

class CMb ...

std::vector< CMb* > myvect(100);

myvect[10] = new CMb();
with a dummy class "class CMb {};" the above code compiles on my gcc 3.3.4.
and so does my VC7 13.10.3052.


std::vector< std::auto_ptr<CMb> > myvect(100);
myvect[10] = new CMb();


I can hardly believe that using std::auto_ptr with a std::vector is safe as
std::auto_ptr does not have
value semantics as recommended for std::vector.
(The implementation of std::vector could use temporary copies, which would
discard all your pointers)

Best Regards
Michael
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: SerGioGio | last post by:
Hello ! When a ref. counted smart pointer (template) class is available (like boost::shared_ptr), is there any (semantic) reason to still use std::auto_ptr rather than this smart pointer ? Or...
1
by: bucher | last post by:
Hi, I want to push a const auto_ptr into a vector, but the compile reports errors. Below is the code. class Folder; class Result; class Results { public: int size(){return _Items.size();}
18
by: puzzlecracker | last post by:
I am using pointers in my program and they obviously make me a bit nervious about possible program states. I am giving serious thoughs about using auto_ptr but unsure whether they can be placed in...
6
by: Jason | last post by:
I have a function (Inet_ntop) that returns const char * and if I try to assign that return value to a char * variable, I get the gcc error message: warning: assignment discards qualifiers from...
8
by: Lloyd Dupont | last post by:
I try to use the auto_ptr in my code but that doesn't work. even std::auto_ptr. I guess I have to add an #include statement, but I can't figure out the right file to add. Also I want to use...
14
by: Andrew | last post by:
Hello all: After spending some time figuring out auto_ptr class' implementation, I decided to write a small article detailing its use of the auto_ptr_ref proxy class to enable construction and...
13
by: Shark | last post by:
Hi, I was reading on http://www.kuro5hin.org/story/2005/10/31/53857/831 that auto_ptr uses "delete" in its destructor instead of delete. The article is fairly recent, so I was wondering if this is...
2
by: Markus Dehmann | last post by:
I am trying to make a pair with a string and an auto_ptr: #include <iostream> #include <map> using namespace std; int main(){ auto_ptr<intp(new int(3)); make_pair("x",p); }
22
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.