Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ invalid conversion

24 views
Skip to first unread message

Heitber Andrés Montilla Ramirez

unread,
Oct 26, 2021, 9:24:35 PM10/26/21
to
Hi everyone I want to know why this error apperars to my code, I'm trying to do an OpenGL App Using codeblocks, I re-checked the code twice with the original tutorial and everything is ok.
now the compiler throws the following error:

|error: invalid conversion from 'const void*' to 'HANDLE {aka void*}' [-fpermissive]|

I want your help to solve this issue.



#define GLUT_DISABLE_ATEXIT_HACK
#define GLEW_STATIC

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif

#include <thread>
#include <string>
#include <stdio.h>
#include <GL/glew.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/glut.h>
#include <GL/gl.h>

#endif
#include "math_3d.h"


GLuint VBO;


static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);

glBindBuffer(GL_ARRAY_BUFFER, VBO);

glEnableVertexAttribArray(0);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(0);

glutSwapBuffers();
}

static void CreateVertexBuffer()
{
Vector3f Vertices[3];
Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
Vertices[1] = Vector3f(1.0f, -1.0f, 0.0f);
Vertices[2] = Vector3f(0.0f, 1.0f, 0.0f);

glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}

static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType)
{
GLuint ShaderObj = glCreateShader(ShaderType);

if(ShaderObj == 0){
fprintf(stderr, "Error Creating shader type %d\n", ShaderType);
exit(0);
}

const GLchar* p[1];
p[0] = pShaderText;

GLint Lengths[1];
Lengths[0] = (GLint)strlen(pShaderText);

glShaderSource(ShaderObj, 1, p, Lengths);

glCompileShader(ShaderObj);

GLint success;
glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success);

if(!success){
GLchar InfoLog[1024];
glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog);
fprintf(stderr, "Error compiling shader type %d: '%s'\n", ShaderType, InfoLog);
exit(1);
}

glAttachShader(ShaderProgram, ShaderObj);
}


const char* pVSFileName = "shader.vs";
const char* pFSFileName = "shader.fs";

static void CompileShaders()
{
GLuint ShaderProgram = glCreateProgram();

if(ShaderProgram == 0){
fprintf(stderr, "Error creating shader Program\n");
exit(1);
}

std::string vs, fs;

if (!ReadFile(pVSFileName, vs)){
exit(1);
};

AddShader(ShaderProgram, vs.c_str(), GL_VERTEX_SHADER);

if(!ReadFile(pFSFileName, fs)){
exit(1);
};

AddShader(ShaderProgram, fs.c_str(), GL_FRAGMENT_SHADER);

GLint Success = 0;
GLchar ErrorLog[1024] = { 0 };

glLinkProgram(ShaderProgram);

glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success);
if(Success == 0){
glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog);
exit(1);
}

glValidateProgram(ShaderProgram);
glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &Success);
if(!Success){
glGetProgramInfoLog(ShaderProgram, sizeof(ErrorLog), NULL, ErrorLog);
fprintf(stderr, "Invalid shader program: '%s'\n", ErrorLog);
exit(1);
}

glUseProgram(ShaderProgram);

}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
int width = 920;
int height = 800;
glutInitWindowSize(width, height);

int x = 200;
int y = 100;
glutInitWindowPosition(x, y);
int win = glutCreateWindow("Tutorial 03");
printf("window id: %d\n", win);

GLenum res = glewInit();
if (res != GLEW_OK){
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}

GLclampf Red = 0.0f, Green = 0.0f, Blue = 0.0f, Alpha = 0.0f;
glClearColor(Red, Green, Blue, Alpha);

CreateVertexBuffer();

glutDisplayFunc(RenderSceneCB);

glutMainLoop();

return 0;

}



Ben Bacarisse

unread,
Oct 26, 2021, 9:51:52 PM10/26/21
to
Heitber Andrés Montilla Ramirez <montilla...@gmail.com> writes:

> Hi everyone I want to know why this error apperars to my code, I'm
> trying to do an OpenGL App Using codeblocks, I re-checked the code
> twice with the original tutorial and everything is ok. now the
> compiler throws the following error:
>
> |error: invalid conversion from 'const void*' to 'HANDLE {aka void*}'
> |[-fpermissive]|
>
> I want your help to solve this issue.

Well, you can get round the problem by using const_cast:

HANDLE h = const_cast<HANDLE>(...);

but it might well pay to find out why the conversion from a pointer to
const to a pointer to non-const is happening. It is potentially unsafe,
so finding out what's really going on is a good idea.

And it's possible that everything should simply be a HANDLE, and the
mistake is to have any const void * pointers at all.

If you tell people where the error occurs, you might get more help on
the details. (I don't have all the right headers installed, so I can't
use a compiler to tell me where the problem is.)

--
Ben.
0 new messages