C++ SDL 2.0 - Importing multiple textures using a loop -


i don't know whether or not possible have used technique in different languages struggling use in c++. have 10 images trying load array using loop so:

for (int = 0; < 10; i++) {     sprite[i] = img_loadtexture(renderer, "graphics/player" + + ".png"); } 

this not seem work in c++ wondering doing wrong, or can same result without having load each image individually so:

sprite[0] = img_loadtexture(renderer, "graphics/player0.png"); 

my error is: "expression must have integral or unscoped enum type"

thanks =)

you cannot this:

"this number: " + (int)4 + "!";

this illegal. give error trying operator+ const char* , const char[some_int_goes_here] or error trying use operator+ add int onto string. things don't work way.

you'd either have use c (i.e. snprintf()) or string stream. here's test code isolating problem:

#include <iostream> #include <string>  int main() {         int = 1;         std::string str = "blah";         std::string end =  "!";          //std::string hello = str + + end;// gives error operator+         std::string hello = "blah" + + "!";          //const char* c_str = "blah" + + "end";         //std::cout << c_str << std::endl;         std::cout << hello << std::endl;         return 0; } 

here's alternative solution using string streams.

#include <iostream> #include <string> #include <sstream>  int main() {     int = 0;     std::string str;     std::stringstream ss;      while (i < 10)     {         //send text string stream.         ss << "text" << i;          //set string text inside string stream         str = ss.str();          //print out string         std::cout << str << std::endl;          //ss.clear() doesn't work. calling constructor         //for std::string() , setting ss.str(std::string())         //will set string stream empty string.         ss.str(std::string());          //remember increment variable inside of while{}         ++i;     } }  

alternatively, can use std::to_string() if you're using c++11 (which requires -std=c++11) std::to_string() broken on sets of compilers (i.e. regular mingw). either switch flavor works (i.e. mingw-w64) or write own to_string() function using string streams behind scenes.

snprintf() may fastest way of doing such thing, safer c++ , better style, recommended use non-c way of doing things.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -