Hello. I am new to this forum. I have a problem with a program I am writing. I have narrowed it down to the following section of code as when I comment this code out my program works fine. The type of error sorry is a linker error. Thanks.
void ItemManager::removeFromList(Item* it)
{
for (iter = itemList.begin(); iter < itemList.end(); iter++)
{
if(it->getSType() == (*iter)->getSType())
itemList.erase(iter);
}
}
Item* ItemManager::getItem(Item* it)
{
for (iter = itemList.begin(); iter < itemList.end(); iter++)
{
if(it->getSType() == (*iter)->getSType())
return (*iter);
}
}
¿You know what really could help? That you tell us what the error is.
I guess that itemList is some kind of container of pointers (I really hope that you are using polymorphism) that is a member of your class.
But it makes no sense to make 'iter' a member. Keep it as a temporary.
By the way itemList.erase(iter); will invalidate the iterator.
Error 2 error LNK2001: unresolved external symbol __imp___CrtDbgReportW
I commented out the itemList.erase(iter). Thanks. I still have the above error though. I also get a warning for the second function "not all control paths return a function". The iterator is being declared as a member of the class in itemmanager.h. Thanks. With the first function my intention was to find the member of the vector to be deleted and remove only that one from the vector. I was wondering if you guys know of a way to do this?
What if you don't find the element. You will return garbage.
1 2 3 4 5 6 7
Item* ItemManager::getItem(Item* it){
for (iter = itemList.begin(); iter < itemList.end(); iter++){
if(it->getSType() == (*iter)->getSType())
return (*iter);
}
return NULL; //if it wasn't found
}
With the first function my intention was to find the member of the vector to be deleted and remove only that one from the vector
As soon as you found the element, you delete it with and return from the function. Again, iter shouldn't be a member, it is just an auxilliar.
I guest that you are working with smart pointers.
Abuot the linker error. Sorry, I've got no idea.
I remember a threat that was similar. It was something about trying to obtain debug info in a release build.
¿Are you using an extern library? Try t make a debug project (or link against the release version)
I was thinking about moving the item object to the back of the vector after I find it then just use list.pushback(it). for example. I was wondering if a list would be better than a vector to achieve this. I am very unfamiliar with vectors and lists btw.