C++ C++;-无法更改向量中的特定向量<;常量GLchar*>;
我正在用opengl制作一个游戏,在将字符串转换为常量字符*时遇到了问题。这是我的密码:C++ C++;-无法更改向量中的特定向量<;常量GLchar*>;,c++,opengl,vector,C++,Opengl,Vector,我正在用opengl制作一个游戏,在将字符串转换为常量字符*时遇到了问题。这是我的密码: for (GLuint i = 0; i < faces.size(); i++) { string fileName(faces[i]), texture = "../content/skybox/" + fileName + ".jpg"; faces[i] = texture
for (GLuint i = 0; i < faces.size(); i++)
{
string fileName(faces[i]),
texture = "../content/skybox/" + fileName + ".jpg";
faces[i] = texture.c_str();
}
for(GLuint i=0;i
不幸的是,一旦跑掉,脸就变得一团糟 一旦退出循环,您的
std::string纹理
就超出范围,它的析构函数将被调用,faces[i]
指向的内存将无效
只需使用std::vector
在站点上注意:不要这样做。您有未定义的行为:
Texture AssetController::LoadCubeMapTexture(vector<const GLchar*> faces, string ID)
{
for (GLuint i = 0; i < faces.size(); i++)
{
string fileName(faces[i]),
texture = "../content/skybox/" + fileName + ".jpg";
// !!!! texture is a local variable and will be
// destroyed once its scope ends (which is
// on next for iteration, or when for ends).
faces[i] = texture.c_str();
}
Texture AssetController::LoadCubeMapTexture(向量面,字符串ID)
{
对于(GLuint i=0;i
解决方案是将接口更改为返回文件名向量:
Texture AssetController::LoadCubeMapTexture(vector<const GLchar*> faces, string ID,
std::vector<std::string>& facesFileNames)
{
for (GLuint i = 0; i < faces.size(); i++)
{
string fileName(faces[i]);
std::string texture = "../content/skybox/" + fileName + ".jpg";
facesFileNames.emplace_back(texture);
}
Texture AssetController::LoadCubeMapTexture(向量面、字符串ID、,
标准::矢量和面文件名)
{
对于(GLuint i=0;i