C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

resizing vector via a pointer to the vector

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Sean Dettrick
Guest





PostPosted: Wed Jul 30, 2003 8:59 pm    Post subject: resizing vector via a pointer to the vector Reply with quote



Hi,
Can anyone please tell me if the following is possible, and
if so, what is the correct syntax?

I'd like to resize a vector (and access other vector member functions)
via a pointer to that vector, e.g:
vector<int> x(10);
vector<int>* p = &x;
*p.resize( 5 );
cout << *p[0] << endl;
But the compiler throws an error: "request for member `resize' in
`p', which is of non-aggregate type `vector *'"

How do I dereference p to obtain an aggregate type?

Here's my complete code:
#include<vector>
#include<iostream>
using namespace std;
int main(){

// some data vectors:
vector<int> x( 10, 1 );
vector<float> y( 10, 2.);
vector<double> z( 10, 3.);

// try to resize via a pointer to the vector:
// (doesn't work)
vector<int>* p = &x;
*(p).resize( 5 );
cout << *(p)[0] << endl;

// a vector of pointers to the data vectors:
int Nentries=3;
vector< void* > entries(Nentries);
entries[0] = &x;
entries[1] = &y;
entries[2] = &z;

// try to resize each of the data vectors via the pointers:
// (doesn't work)
for ( int entry=0; entry
}

Any suggestions?
Thanks very much,
Sean
Back to top
Victor Bazarov
Guest





PostPosted: Wed Jul 30, 2003 9:07 pm    Post subject: Re: resizing vector via a pointer to the vector Reply with quote



"Sean Dettrick" <sdettrick (AT) hotmail (DOT) com> wrote...
Quote:
Hi,
Can anyone please tell me if the following is possible, and
if so, what is the correct syntax?

I'd like to resize a vector (and access other vector member functions)
via a pointer to that vector, e.g:
vector<int> x(10);
vector<int>* p = &x;
*p.resize( 5 );
cout << *p[0] << endl;
But the compiler throws an error: "request for member `resize' in
`p', which is of non-aggregate type `vector *'"

How do I dereference p to obtain an aggregate type?

(*p).resize(5);

or

p->resize(5);

the same goes for the operator []. If you want to reach
the zeroth element of the vector pointed to by 'p', you
need to put *p in parentheses:

(*p)[0]

otherwise you're indexing the vector from the pointer and
then trying to dereference the vector (doesn't compile):

*(p[0])

Learn the precedence of operators.

Victor



Back to top
Sean Dettrick
Guest





PostPosted: Thu Jul 31, 2003 3:52 am    Post subject: Re: resizing vector via a pointer to the vector Reply with quote



Thanks very much!
Sean
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.