std::vector reference problem

Started by
4 comments, last by Zahlman 14 years, 1 month ago
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 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
Advertisement
// Change:Item2 item = reinterpret_cast<Item2>(items);// Here, the subscript operator treats items as an array of std::vector<Item2>s// To:Item2 item = (*items);// 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[]
	std::vector<Item2>* list = t.getItems();	Item2 item = (*list)[0];

sry was to late
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.
thanks guys, I tried it without the brackets I'm sure and it didn't work before (I'm guessing because it was treating it as *(items) rather than (*items) )
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.

This topic is closed to new replies.

Advertisement