• Advertisement

Archived

This topic is now archived and is closed to further replies.

A pointer to a vector.

This topic is 5268 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Yeah, just realized that.

Thanks for the quick response. I didn''t realize that -> was just eyecandy for (*pointer).member.

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites


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

Share this post


Link to post
Share on other sites
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) .

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites

  • Advertisement