pointer to a vector of pointers

U

uotani.arisa

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!
 
U

uotani.arisa

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!
 
D

deane_gavin

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-lite/how-to-post.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
 
U

uotani.arisa

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!
 
P

Phil Staite

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.
 
U

uotani.arisa

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...
 
U

uotani.arisa

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...
 
O

Old Wolf

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?
 
A

Axter

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 )
{
}
 
U

uotani.arisa

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!
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top