Public Group

# std::vector reference problem

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

## 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 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 on other sites
// 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[]

##### Share on other sites
	std::vector<Item2>* list = t.getItems();	Item2 item = (*list)[0];

sry was to late

##### 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 on other sites
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) )

##### 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.

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631782
• Total Posts
3002332
×