Sign in to follow this  

std::vector reference problem

This topic is 2835 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

I have a vector of objects of type Item2 being populated within my Map class, but when my main game attempts to use the items of this vector through a getter method I seem to be losing the type of the vector
const std::vector<Item2> *Map::getItems()
{
	
	return &items;
}

and I'm trying to access it via
std::vector<Item2>* items = game->getMap().getItems();
Item2 item = reinterpret_cast<Item2>(items[i]);

I wasn't originally using the reinterpret_cast as I know it's unsafe but without it I was getting the error: 'initializing' : cannot convert from 'std::vector<_Ty>' to 'Item2' 1> with 1> [ 1> _Ty=Item2 1> ] 1> No constructor could take the source type, or constructor overload resolution was ambiguous How do I prevent the type of a vector's contents being lost when getting it from an outside method? cheers, sepharion

Share this post


Link to post
Share on other sites

// Change:
Item2 item = reinterpret_cast<Item2>(items[i]);
// Here, the subscript operator treats items as an array of std::vector<Item2>s

// To:
Item2 item = (*items)[i];
// Here, std::vector<Item2>* is converted to std::vector<Item2>& through the dereference operator
// and the subscript operator calls the appropriate std::vector<Item2>::operator[]


Share this post


Link to post
Share on other sites
If you return a (const) reference, then you won't need to dereference the pointer at the caller. Unless you want NULL to be a valid value returned from the function, this is a superior solution:

const std::vector<Item2> &Map::getItems()
{

return items;
}

Alternatively you could have a begin()/end() function pair that returns iterators, depending on what the caller needs to do. Often raw get/set functions are indicators of a poorly designed class.

Share this post


Link to post
Share on other sites
The first problem is, your thread is titled "std::vector reference problem" but you aren't using a reference anywhere. You were creating and returning a pointer.

If you had actually used a reference, none of this would have come up. See rip-off's post for how to use a reference. Notice that the logic is simpler on both sides of the function (implementing it and using it).

Since "items" is a pointer-to-vector-of-Item2, "items[i]" is a vector-of-Item2. Subscripting on a pointer treats it as if you had an array at the pointed-at location in memory.

The error tells you the problem. Your statement is an 'initialization', because you declare a variable and set its value on the same line. The compiler "cannot convert std::vector<Item2> to Item2", which means that the thing you're initializing is an Item2 (obvious) and the value you want to initialize it with is a std::vector<Item2>.

This obviously is not what you wanted to happen.

Whenever you write a cast, you are telling the compiler "shut up, yes that is what I wanted to happen". You should almost never do this. Read error messages.

You thought that the type of the vector's contents was being "lost". There is no such thing as a value "losing" its type in C++, because every value has a type. The only thing that can happen is that the type is mis-interpreted somewhere. You can "lose" type information in the sense that, for example, a derived class might appear to be an instance of the base class instead.

You should have suspected that this shouldn't happen in the current case, though, and if you read the error message you would know it wasn't what was actually happening, because clearly there's no way that the type of the vector's elements could be mis-interpreted as the type of the vector itself.

Share this post


Link to post
Share on other sites

This topic is 2835 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this