473,385 Members | 1,720 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.

pointer to a vector of pointers

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
9 5849
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

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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: john townsley | last post by:
when using vectors with pointers of objects. is it best to create instances then point to them or dynamic allocation upon creating a vector element. I can do this, but is creating a vector with...
18
by: silversurfer | last post by:
Ok, this should be fairly easy for most of you (at least I hope so), but not for me: Let us say we have got the following elements: std::vector<Entry> models; //Entry is a struct...
9
by: JoeC | last post by:
I have a question about advanced poiters. I know the basics of memory managment and pointers. That a pointer points to a memory location. I also have read that dynamic memory new is used for...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
14
by: Glen Dayton | last post by:
While looking at some old code I ran across a snippet that increments a pointer to access different members of a structure: .... struct Badness { std::string m_begin; std::string m_param1;...
21
by: Bo Yang | last post by:
As long as I write c++ code, I am worry about the pointer. The more the system is, the dangerous the pointer is I think. I must pass pointers erverywhere, and is there a way to ensure that every...
5
by: JoeC | last post by:
I am trying to make an iterator work with pointers. I wrote this code and it works but with an upgrade to my program vector is now unit* instead of unit type. void kill(std::vector<unit>& u){...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
18
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.