static const GLchar *c becomes undefined

static const GLchar *c becomes undefined

本文关键字:becomes undefined const GLchar static      更新时间:2023-10-16

shaderLoader函数检索字符串,然后用c_str()将其转换为const char。但由于某种原因,c没有定义。有提示吗?

static const GLchar *  c[1000];
*c =  shaderLoader("C:\Users\Dozent-01\Desktop\User\CG\bin\vertShader.txt").c_str();
glShaderSource(vertex_shader, 1, c, NULL);

在你的代码中包含这个文件

  #include <GL/glew.h>

参考这篇文章

我假设c_str()的行为与std::string::c_str()相同。这并不能转换成。它给你一个指向存储在字符串中的数据的指针,这个指针只有在字符串本身保持活跃和不变的情况下才有效。

我也将假设shaderLoader()返回一个临时对象。你获取一个指向它的数据的指针,并将该指针存储在c[0]中。在该表达式的末尾,临时变量被销毁,因此指针不再指向有效数据。这是悬空。

你必须复制数据,而不仅仅是存储指向它的指针。像这样:

auto str = shaderLoader("whatever");
c[0] = new GLchar[str.size() + 1];
strcpy(c[0], str.c_str());
// don't forget to delete[] the memory when no longer needed

当然,还有一个问题,为什么你有一个指向cosnt GLchar的1000个指针的数组。我怀疑你实际上意味着c是存储字符串的字符缓冲区。如果是这样,您可以这样修改代码:

static const GLchar c[1000];
strncpy(c, shaderLoader("whatever").c_str(), sizeof(c));