By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,501 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,501 IT Pros & Developers. It's quick & easy.

pointer to a vector of pointers

P: n/a
Hi,

Can someone tell me how to declare a pointer to a vector of pointers?
I'm just not sure how to do this...

I've tried essentially the following:

vector<string *> * v;

....

void foo( const string a1, vector<string *> * a2 )
{
....
}

and I get the following error:

error C2664: 'foo' : cannot convert parameter 2 from
'class std::vector<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *> > *'
to
'class std::vector<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *> >'

Any help would be greatly appreciated... Thanks!

Nov 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
opps... let me update the code segments:

vector<string *> * v;
string s = new string ("blar");
foo( s, v);
...

void foo( const string a1, vector<string *> * a2 )
{
...
}

thanks!

uo**********@gmail.com wrote:
Hi,

Can someone tell me how to declare a pointer to a vector of pointers?
I'm just not sure how to do this...

I've tried essentially the following:

vector<string *> * v;

...

void foo( const string a1, vector<string *> * a2 )
{
...
}

and I get the following error:

error C2664: 'foo' : cannot convert parameter 2 from
'class std::vector<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *> > *'
to
'class std::vector<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *> >'

Any help would be greatly appreciated... Thanks!


Nov 22 '05 #2

P: n/a

uotani.ar...@gmail.com wrote:
Hi,

Can someone tell me how to declare a pointer to a vector of pointers?
I'm just not sure how to do this...

I've tried essentially the following:

vector<string *> * v;

...

void foo( const string a1, vector<string *> * a2 )
{
...
}

and I get the following error:

error C2664: 'foo' : cannot convert parameter 2 from
'class std::vector<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *> > *'
to
'class std::vector<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *> >'

Any help would be greatly appreciated... Thanks!


You'll need to post more code, following the guidelines in

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

I put the two lines you say are causing your problem into this program

#include <vector>
#include <string>

void foo(const std::string a1, std::vector<std::string *> * a2 )
{}

int main()
{
std::string s;
std::vector<std::string *> * v;

// assume v is sensibly intialised.
foo(s, v);

return 0;
}

and it compiles with no errors on Comeau online, so there must be more
to your problem than you have shown.

Gavin Deane

Nov 22 '05 #3

P: n/a
oh wow, thanks! i'm still in the process of determining where my error
is. it's good to know that i can declare it like that. it was my first
time posting here. i'll read the faq carefully and make sure i follow
it when posting again. thanks again!

Nov 22 '05 #4

P: n/a
uo**********@gmail.com wrote:
opps... let me update the code segments:

vector<string *> * v;
string s = new string ("blar");
foo( s, v);
...

void foo( const string a1, vector<string *> * a2 )
{
...
}

thanks!

Since you didn't post all your code, and I didn't grovel through the
entire error message, I'll have to guess but...

First off, I don't see you allocating a vector. All you've posted shows
that you have an uninitialized pointer to a vector... Crash #1 in your
future.

Back to the compile error. Are you trying to assign the address of
*const* string a1 into your vector of pointers to *non-const* strings???
Do you see a problem? :-) That is probably what the (rather wordy)
compiler error is.

The address-of a1 will be a pointer to a const string, and you cannot
assign it into a vector of pointers to non-const strings. Otherwise
you'd be able to modify the const string through the non-const pointer
to it in the vector. That's what the compiler is trying to help you
prevent.

What it is really preventing you from doing is making a huge mistake.
(Crash #2) Note that your a1 is passed by value. Therefore it exists
only within the scope of the function. If you stuff a pointer to it
into your vector and allow the caller to try to access it... boom.

So do not try to cast away const-ness via const_cast<>... Do not change
the declaration of a1 to be non-const... Do not change a2 to be a
pointer to a vector of pointers to const strings. All of these would
compile, but lead to serious problems later.

What you may want to do is make the vector hold strings by value and put
a copy of a1 in it. Or maybe explicitly get a copy of a1 via new and a
copy constructor. (but then you have to manage the memory in a2)...
What you really want to do is re-think what you're trying to do here.
Nov 22 '05 #5

P: n/a
ugh, embarassing... yes, i forgot to initialize the vector. it's
working now... thanks a lot, phil! hmm... this forum is great! next
time, i'll try to post something more interesting...

Nov 22 '05 #6

P: n/a
Just to be a little more thorough, I also forgot to update one of my
function prototypes, which I fixed as well to get the code running. It
had incorrect parameter types... way to go me. Had I included all my
code, that would have been obvious as well. Hmm... I see the reasoning
behind the rules for this forum. Thanks again for the input, and thanks
for putting up with my noobiness...

Nov 22 '05 #7

P: n/a
uo**********@gmail.com wrote:
opps... let me update the code segments:
Please quote previous posts when replying -- not everyone is
using an interface where they can see what you're replying to.

vector<string *> * v;
string s = new string ("blar");
This line doesn't compile (but the error message would not
be what you pasted either). Please write a complete program
that gives the error message, and copy-and-paste that exact
program here.

'new X' returns a pointer to X. You can't assign pointers
to non-pointers. Either write:
string *s = new string("blar");
or
string s("blar");
void foo( const string a1, vector<string *> * a2 )


Your error message corresponds to what would happen if
you left off the last '*' in this line, or put in an extra * when
calling the function (but there are of course other possible
causes).

Finally, a pointer to a vector of pointers to string, is an
unusual beast -- why don't you use a vector of strings?

Nov 22 '05 #8

P: n/a
uo**********@gmail.com wrote:
Hi,

Can someone tell me how to declare a pointer to a vector of pointers?
I'm just not sure how to do this...

I've tried essentially the following:

vector<string *> * v;

...

void foo( const string a1, vector<string *> * a2 )
{
...
}

and I get the following error:

error C2664: 'foo' : cannot convert parameter 2 from
'class std::vector<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *> > *'
to
'class std::vector<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > *> >'

Any help would be greatly appreciated... Thanks!


It seems as though your code is over using pointers.
You should only use pointers when you're sure you have to, and you
should avoid using raw pointers in a vector.
Instead, either use a concrete type, or use a smart pointer in your
vector.
vector<string> v;
//And pass by reference instead
void foo( const string a1, vector<string> &a2 )
{
}

If you're sure you need to use a pointer in your vector, then use a
smart pointer like the boost::shared_ptr or the following clone smart
pointer:
http://code.axter.com/copy_ptr.h
//Example usage
vector<copy_ptr<string> > v;

void foo( const string a1, copy_ptr<string> > &a2 )
{
}

Nov 22 '05 #9

P: n/a
First of all, I want to thank everyone for their input. This has been a
helpful experience.
Finally, a pointer to a vector of pointers to string, is an
unusual beast -- why don't you use a vector of strings?


The only reason why I don't use a vector of strings is because I'm
working with already existing functions that take in a vector of string
pointers. Here's essentially what I ended up with:

#include <vector>
#include <string>

void foo( const std::string a1, std::vector<std::string *> * a2 );

int main()
{
std::vector<string *> v;
std::string s("blar");
foo( s, &v);

// Pass &v on to some existing functions for further processing.

return 0;
}

void foo( const std::string a1, std::vector<std::string *> * a2 )
{
// ... some code here that populates a2
}

If there's time in the future, I'll talk with some people about
rewriting the existing functions to *not* take in a vector of string
pointers... I know it'd be easier for me to think of just a vector of
strings, as was suggested by many people here so far. Again, thanks a
lot!

Nov 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.