Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ SDL 2——使用循环导入多个纹理 我不知道这是否可能,但我已经用不同的语言使用了这个技术,但是我努力在C++中使用它。我有10个图像,我正试图使用循环加载到阵列中,如下所示: for (int i = 0; i < 10; i++) { Sprite[i] = IMG_LoadTexture(renderer, "Graphics/Player" + i + ".png"); }_C++ - Fatal编程技术网

C++ SDL 2——使用循环导入多个纹理 我不知道这是否可能,但我已经用不同的语言使用了这个技术,但是我努力在C++中使用它。我有10个图像,我正试图使用循环加载到阵列中,如下所示: for (int i = 0; i < 10; i++) { Sprite[i] = IMG_LoadTexture(renderer, "Graphics/Player" + i + ".png"); }

C++ SDL 2——使用循环导入多个纹理 我不知道这是否可能,但我已经用不同的语言使用了这个技术,但是我努力在C++中使用它。我有10个图像,我正试图使用循环加载到阵列中,如下所示: for (int i = 0; i < 10; i++) { Sprite[i] = IMG_LoadTexture(renderer, "Graphics/Player" + i + ".png"); },c++,C++,我的错误是:表达式必须具有整数或非范围枚举类型 感谢您的帮助=您不能这样做: 这是我的号码:+int4+ 这是违法的。如果尝试使用运算符+常量字符*和常量字符[SOME_INT_GOES_HERE],则会出现错误;如果尝试使用运算符+将INT添加到字符串中,则会出现另一个错误。事情不是这样的 您必须使用C.e.snprintf或字符串流。以下是我隔离问题的测试代码: #include <iostream> #include <string> int main() {

我的错误是:表达式必须具有整数或非范围枚举类型

感谢您的帮助=

您不能这样做: 这是我的号码:+int4+

这是违法的。如果尝试使用运算符+常量字符*和常量字符[SOME_INT_GOES_HERE],则会出现错误;如果尝试使用运算符+将INT添加到字符串中,则会出现另一个错误。事情不是这样的

您必须使用C.e.snprintf或字符串流。以下是我隔离问题的测试代码:

#include <iostream>
#include <string>

int main()
{
        int a = 1;
        std::string str = "blah";
        std::string end =  "!";

        //std::string hello = str + a + end;// GIVES AN ERROR for operator+
        std::string hello = "blah" + a + "!";
      
        //const char* c_str = "blah" + a + "end";
        //std::cout << c_str << std::endl;
        std::cout << hello << std::endl;
        return 0;
}
这里有一个使用字符串流的替代解决方案

或者,如果您使用的是C++11,则也可以使用std::to_字符串,因为C++11只需要-std=C++11,但std::to_字符串在某些编译器集(即常规MinGW)上被破坏。要么切换到另一种风格,即MinGW-w64,要么在幕后使用字符串流编写自己的to_字符串函数


SNPRTFF可能是最快的方法,但是对于更安全的C++和更好的样式,建议使用非C的方式来做。

< P>我有类似的问题,我用这样的方式解决:

#include <iostream>
using namespace std;

int main() {
   string line;

   for (int i = 0; i < 10; i++) {
       line = "Graphics/Player" + inttostr(i) + ".png"; //I wrote inttostr function because built in inttostr functions messed up my program (see below)
       char charger[line.length()]; //creating char array
       for (int i = 0; i < sizeof(line); i++) {
           charger[i] = line[i]; // copying string to char arry
       }
       Sprite[i] = IMG_LoadTexture(renderer, charger);
    }   
}

string inttostr(int integer) { //I know it isn't the best way to convert integer to string, but it works
    string charakter;
    int swap;
    bool negativ = false;

    if (integer < 0) {
        integer = -integer;
        negativ = true;
    }
    if (integer == 0) {
        charakter = "0";
    }
    while (integer >= 1) {
        swap = integer % 10;
        integer = integer / 10;
        charakter = char(swap + 48) + charakter;
    }
    if (negativ) {
        charakter = "-" + charakter;
    }
    return charakter;
}

首先,你把for循环搞错了。它代表int i=0;i<10;i++。除此之外,我看不出它有什么问题。是的,我只是注意到了循环,我会把它编辑掉,但当我将I组合到字符串中时,它似乎不喜欢它。它只是创建了一个错误。请详细说明失败的原因,例如编译器错误等。我假设字符串上的+运算符和int是这里的问题,您需要使用类似std::to_string的东西错误正如您在代码的这一部分中所说的:Graphics/Player+i+.png;。我试着在它上面创建一个字符串变量,并在它的位置上播放它,我只是尝试了你所说的转换为字符串,但它似乎仍然不喜欢它。我的错误是:表达式必须有整数或无范围的枚举类型谢谢你的提示,我将研究它=
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    int i = 0;
    std::string str;
    std::stringstream ss;
    
    while (i < 10)
    {
        //Send text to string stream.
        ss << "text" << i;
        
        //Set string to the text inside string stream
        str = ss.str();
        
        //Print out the string
        std::cout << str << std::endl;
        
        //ss.clear() doesn't work. Calling a constructor
        //for std::string() and setting ss.str(std::string())
        //will set the string stream to an empty string.
        ss.str(std::string());
        
        //Remember to increment the variable inside of while{}
        ++i;
    }
} 
#include <iostream>
using namespace std;

int main() {
   string line;

   for (int i = 0; i < 10; i++) {
       line = "Graphics/Player" + inttostr(i) + ".png"; //I wrote inttostr function because built in inttostr functions messed up my program (see below)
       char charger[line.length()]; //creating char array
       for (int i = 0; i < sizeof(line); i++) {
           charger[i] = line[i]; // copying string to char arry
       }
       Sprite[i] = IMG_LoadTexture(renderer, charger);
    }   
}

string inttostr(int integer) { //I know it isn't the best way to convert integer to string, but it works
    string charakter;
    int swap;
    bool negativ = false;

    if (integer < 0) {
        integer = -integer;
        negativ = true;
    }
    if (integer == 0) {
        charakter = "0";
    }
    while (integer >= 1) {
        swap = integer % 10;
        integer = integer / 10;
        charakter = char(swap + 48) + charakter;
    }
    if (negativ) {
        charakter = "-" + charakter;
    }
    return charakter;
}