# C++ Help

## Recommended Posts

Crusable77    594

Hello, I am making a game in C++ and SDL and I am having some iterator/vector trouble(I think). I am loading all my images into a vector of Sprites, which I made so I could associate a std::string with SDL_Surface*. However When I try to get the image I get This error:


Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Sprite' (or there is no acceptable conversion)


Here is the code:


SDL_Surface* MediaRepository::GetSurface(std::string SurfaceName){

mIter = find(mImages.begin(), mImages.end(), SurfaceName);

if(mIter != mImages.end()){

return mIter->GetSurface();
}

return NULL;
}


mImages is the vector of Sprites and GetSurface returns a SDL_Surface*. Thank you in advance for any help.

##### Share on other sites
BitMaster    8651
std::find returns the first iterator it for which *it == SurfaceName is true. Unless your Sprite class has an operator == which takes an std::string as a second argument, that cannot work. I would not advise adding such an operator by the way.

Either use std::find_if with an appropriate functor/lambda expression or use an std::map<std::string, Sprite>.

##### Share on other sites
Serapth    6671

std::find returns the first iterator it for which *it == SurfaceName is true. Unless your Sprite class has an operator == which takes an std::string as a second argument, that cannot work. I would not advise adding such an operator by the way.

Either use std::find_if with an appropriate functor/lambda expression or use an std::map<std::string, Sprite>.

I would squarely suggest doing the latter.  If you are indexing a datatype by string... thats a map.

##### Share on other sites

To be clear, you should use the map INSTEAD of the vector.

##### Share on other sites
Wrathnut    466

For my C++/SDL 2d game I am doing the same sort of thing where I have a manager that keeps a vector list of gracphics objects and can render them according to an ID or a string name. For you to compare string values you have to do a

.compare(value)==0

I store an name, ID and an image type in my graphics object. The ID or name are referencing the owner of the image. The type refers to a standard image type. This can easily be associated with an animation list so that current images from a running animation can be pulled from the library.

Here is one of my routines for rendering images

void gFXManager::renderImage(SDL_Surface *screen, string name, int type, int x, int y){

for(int i=0; i < this->gFXList.size(); i++){
if(this->gFXList[i]->getName().compare(name)==0 && this->gFXList[i]->getType()==type){
//Render the image to the surface
this->gFXList[i]->drawImage(screen, x, y);
break;
}
}
}


##### Share on other sites
Serapth    6671

For my C++/SDL 2d game I am doing the same sort of thing where I have a manager that keeps a vector list of gracphics objects and can render them according to an ID or a string name. For you to compare string values you have to do a

.compare(value)==0

I store an name, ID and an image type in my graphics object. The ID or name are referencing the owner of the image. The type refers to a standard image type. This can easily be associated with an animation list so that current images from a running animation can be pulled from the library.

Here is one of my routines for rendering images

void gFXManager::renderImage(SDL_Surface *screen, string name, int type, int x, int y){

for(int i=0; i < this->gFXList.size(); i++){
if(this->gFXList[i]->getName().compare(name)==0 && this->gFXList[i]->getType()==type){
//Render the image to the surface
this->gFXList[i]->drawImage(screen, x, y);
break;
}
}
}


This code should almost certainly use a map in place of a vector, especially if you have a lot of items.  There are two reasons for this, first the vector will simplify your code and as a result make it less error prone and easier to maintain.  You would eliminate the loop completely.  Second, as complexity grows the search will certainly be faster on a map than a raw iteration over elements.

Small quibbles, you should use an iterator.  Also, generally prefer pre-increment operators ( ++i  ) over post-increment ( i++ )  unless you have a reason to do otherwise.  In a for loop, they will be functionally identical, but the use of a post increment can result in a variable being creating each iteration, depending on your compiler.  I think most are smart enough to deal with this, but it's a low hanging fruit thing to get in the habit of changing.

##### Share on other sites
Wrathnut    466

I might beg to differ a bit. But it is only a minor quible. My system has the flexibility to look up versus name or ID and as simple name value pair isn't quite what I desire. I am comparing several things in my search not just a name. That and I am not particlulary fond of the syntax for iterators. But that is preference as far as I am concerned.

If it works and gives you the performance you need then use it.  A map or a vector is just the vehicle to get the job done.

BitMaster answered the question as to why it was not working I was just showing him how he can use his current system by just comparing a string value.

I would like to ask how my imlementation is more error prone. I am assuming you meant to say, "There are two reasons for this, first the map will simplify your code and as a result make it less error prone and easier to maintain."

I am not try to start an argument but I am always trying to learn new things or techniques.

##### Share on other sites
Serapth    6671

I would like to ask how my imlementation is more error prone. I am assuming you meant to say, "There are two reasons for this, first the map will simplify your code and as a result make it less error prone and easier to maintain."

Basically it boils down to any time you are writing code to make your datatype do what another data type does better out of the box, that additional code is more prone to be buggy.  It's an extension of the logic, the STL datatypes have been viewed by thousands of developers and tested by millions, so unless you have a very good reason, you shouldn't roll your own.  You are essentially adapting a vector to be a datatype it isn't meant to be.  ( Not that your code is wrong or bad, it's short and simple enough that it really doesn't matter, but in a more complex system it becomes more important ).

In your particular case, if you want to have two keys, one of string and one of int say, you would make your map of type std::map<std::pair<int,string>,Sprite>;  I know the syntax looks unwieldy at first ( like iterators ), but this is the way of the future with C++ ( but thank god for auto! ). There are behind the scenes advantages to each type as well, and if you have two keys, the map will certainly perform better once you hit a very small threshold ( maybe 12 items or so... ).  Now if you require certain traits, like guaranteed order or compactness, vector may be a better choice.

##### Share on other sites
EWClay    659
With a map the function would look something like this, and it would not get slower as the list gets larger. I think it's an improvement. You can define comparison or hash functions, so there shouldn't be any issue over flexibility.

void gFXManager::renderImage(SDL_Surface *screen, string name, int type, int x, int y){

auto it = gFXMap.find(KeyType(name, type));

if (it != gFXMap.end()) {

//Render the image to the surface
it->second->drawImage(screen, x, y);
}
}


##### Share on other sites
Wrathnut    466

Serapth I actually did look into using maps when I was first designing my systems apparently I didn't look hard enough at maps. I am genuinely surprised that it doesn't get slower as the list gets larger I will have to look into it a bit more.

And Thank you EwClay for the example.

##### Share on other sites
Serapth    6671
Oh, it still gets slower as it grows, it just does it at a lower pace.

##### Share on other sites
EWClay    659

Oh, it still gets slower as it grows, it just does it at a lower pace.

Tree based maps are O(logN), which is pretty slow growth. But hash maps are constant time, allowing for the occasional resize.