A pointer to a vector.

Started by
6 comments, last by Kranar 20 years, 8 months ago
How can one use the [] referencing operator when working with a pointer to a vector of objects? For example, I would like to do something like the following:

vector<OBJECT>* index;

if(type == 1) {
  index = &room[target.roomid].obj;
} else if(type == 2) {
  index = &character[target.charid].obj;
}

if(index[id]->is_container) {
   index[id]->container[0].curnum = 0;
   index[id]->container[0].volume = 0;
}
if(index[id]->is_counter) {
   index[id]->counter[0].curnum = 0;
   index[id]->counter[0].volume = 0;
}
if(index[id].is_rug) {
   index[id]->rug[0].curnum = 0;
   index[id]->rug[0].volume = 0;
}
However, I get this error:
   
c:\objects.cpp(498): error C2819: type 'std::vector<_Ty,_Ax>' does not have an overloaded member 'operator ->'
        with
        [
            _Ty=OBJECT,
            _Ax=std::allocator<OBJECT>
        ]
Any help on how I can use a pointer to a vector of objects would be much appreciated. Thank you. [edited by - Kranar on August 19, 2003 6:48:37 AM]
Advertisement
You have to dereference index first -
e.g.

if((*index)[id].is_container)
...

For the compiler, index could also be an array.
So whenn you use the index operator on it, a wrong memory address would be accessed.
Then you try to use -> operator on an already dereferenced object which also hasn't the -> operator implemented -
this generates the compiler error.

BlackHC

[edited by - BlackHC on August 19, 2003 6:58:23 AM]
I do know that I don't know anything.
If ''index'' is a pointer to a std::vector, index[id] will give you the id''th vector index is pointing to.

That is:

index[id] evaluates to (std::vector).

So your line
index[id]->container[0].curnum = 0;

Looks like

(std::vector)->container[0].curnum = 0;
// Hence the error std::vector does not have -> defined.

Maybe you want to do this:

index[id][object]->container[0].curnum ?

Problem is, I don''t know what OBJECT is... index is also only pointing to one vector... so doing index[id] with any other id then 0 will result in an error...

Maybe you''re also trying to do this:
(*index)[id]-> ... ?
//Get me whatever index is pointing to (std::vector) now use [id] on that.
How do I set my laser printer on stun?
Yeah, just realized that.

Thanks for the quick response. I didn''t realize that -> was just eyecandy for (*pointer).member.
You must first dereference the pointer, so that you can then apply the subscript operator to the std::vector object it points to.
std::vector<T> v;// fill std::vector<T>* pv = &v // (assuming there is an overloaded operator `std::ostream& operator<<(std::ostream&, const T&)'')std::cout << (*pv)[ix] << std::endl;


[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]

vector<OBJECT> & index = (type == 1) ? room[target.roomid].obj : character[target.charid].obj;

Just for future reference, you can also use the at() member function of std::vector, like so:

vector<OBJECT> *index;...if (index->at(id).is_container) {...}


at(n) returns a reference to the object at position n in the vector, just the same as operator[](n) .
quote:Original post by fizban75
at(n) returns a reference to the object at position n in the vector, just the same as operator[](n) .

They are not exactly the same; std::vector::at() provides range checking, and throws a std::out_of_range (exception) if the index is out of range. It is therefore less efficient, but safer. You would use a try block and catch clause with the code you presented.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]

This topic is closed to new replies.

Advertisement