 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Sean Dettrick Guest
|
Posted: Wed Jul 30, 2003 8:59 pm Post subject: resizing vector via a pointer to the vector |
|
|
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
|
Posted: Wed Jul 30, 2003 9:07 pm Post subject: Re: resizing vector via a pointer to the vector |
|
|
"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
|
Posted: Thu Jul 31, 2003 3:52 am Post subject: Re: resizing vector via a pointer to the vector |
|
|
Thanks very much!
Sean
|
|
| Back to top |
|
 |
|
|
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
|
|