static const GLchar *c becomes undefined
static const GLchar *c becomes undefined
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));
相关文章:
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- "Undefined class"作为is_base_of的论据
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- VSCode C/C++ Intellisense issue: Undefined identifiers (Linu
- 与卡特琳娜一起卷曲C++失败并得到"Undefined symbols for architecture x86_64"
- "Undefined symbols for architecture x86_64" 在 SDL 2 (macOS) 中
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- 从 C 可执行文件加载动态库时收到错误C++"undefined symbol"
- 为什么即使在定义之后仍存在"Identifier is undefined error "?
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- 为什么当我在 C 程序中链接静态库时会"undefined symbol"?
- Vector, assign() function Undefined Behavior
- OpenCV imwrite 函数导致'undefined symbol'
- Libcurl c++ "undefined reference to" (Windows/MinGW/g++)
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- build error : undefined reference to `yyFlexLexer::yyFlexLex
- static const GLchar *c becomes undefined