C++ 如何初始化常量字符**数组

C++ 如何初始化常量字符**数组,c++,opengl-es-2.0,C++,Opengl Es 2.0,这是我的密码 这是头文件 class ShaderManager { private: const GLchar** vertexShaderCode_color; const GLchar** fragmentShaderCode_color; const GLchar** vertexShaderCode_texture; const GLchar** fragmentShaderCode_texture; ShaderManager(); ~ShaderManager(); cpp文件

这是我的密码

这是头文件

class ShaderManager {
private:

const GLchar** vertexShaderCode_color;
const GLchar** fragmentShaderCode_color;
const GLchar** vertexShaderCode_texture;
const GLchar** fragmentShaderCode_texture;
ShaderManager();
~ShaderManager();
cpp文件。在构造函数中

vertexShaderCode_color = {
    "uniform mat4 uMVPMatrix;\n"
    "attribute vec4 vPosition;\n"
    "void main() { \n"
    "   gl_Position = uMVPMatrix * vPosition; \n"
    "}\n"
};
fragmentShaderCode_color = {
    "precision mediump float;\n"
    " uniform vec4 vColor;\n"
    "void main() {\n"
    "gl_FragColor = vColor;\n"
    "}\n"
};
vertexShaderCode_texture = {
    "uniform mat4 uMVPMatrix;\n"
    "attribute vec4 vPosition;\n"
    "attribute vec2 texCoord;\n"
    " varying vec2 texCoordOut;\n"
    "void main() {\n"
    "gl_Position = uMVPMatrix * vPosition;\n"
    "texCoordOut = texCoord;\n"
    "}\n"
};
fragmentShaderCode_texture = {
    "precision mediump float;\n"
    "varying vec2 texCoordOut;\n"
    "uniform sampler2D u_texture;\n"
    "uniform vec4 vColor;\n"
    " void main() {\n"
    "  vec4 texColor = texture2D(u_texture, texCoordOut);\n"
    "  gl_FragColor = texColor;\n"
    "}\n"
};
它不起作用了。错误消息是:

错误3错误:无法在分配中将“”转换为“const GLchar**{aka const char**}” D:\workspace\VS2013\Projects\bbEngine2\bbEngine2\src\GLManager\ShaderManager.cpp 10 2 bbEngine2


我想你需要这样的东西:

static const char* vertexShaderCode_color_value[] = {
    "uniform mat4 uMVPMatrix;\n"
    "attribute vec4 vPosition;\n"
    "void main() { \n"
    "   gl_Position = uMVPMatrix * vPosition; \n"
    "}\n"
};
vertexShaderCode_colour = vertexShadeCode_colour_value;

你被C和C++中数组和指针之间的细微差别所打动。 编辑:我认为您需要对变量进行名称空间限定,但我看到这是在构造函数中。。。在这种情况下,如果不能使用静态变量作为值,则需要进行一些内存分配:

类内定义:

std::vector<const GLChar*> vertexShaderCode_color_value;
GLChar** vertexShaderCode_color;  // Order is significant.
(如果愿意,也可以在构造函数体中执行)

如何初始化常量字符**数组

以与其他阵列相同的方式

const GLchar** vertexShaderCode_color;
这不是一个
常量字符**
数组。它是指向
常量GLchar*
的单个指针

vertexShaderCode_color = {
    "uniform mat4 uMVPMatrix;\n"
    "attribute vec4 vPosition;\n"
    "void main() { \n"
    "   gl_Position = uMVPMatrix * vPosition; \n"
    "}\n"
};
这确实是初始化数组的方法。但正如我已经指出的,您没有将
vertexShaderCode\u color
声明为数组。这是一个指针。此外,如果它在构造函数主体内,则该成员已经初始化。不能指定给数组

如果确实希望
vertexShaderCode\u color
成为数组,则需要将其声明为数组。请记住,阵列的大小必须在运行时已知:

const char* vertexShaderCode_color[5];
然后在成员初始值设定项列表或默认成员初始值设定项中对其进行初始化

const char* vertexShaderCode_color[5];