I have been trying to draw a triangle to the screen using sdl and glew but genbuffers is throwing an exception. Can anyone please help. Thank you

//This is the code for the MainGame.h part of the class

#pragma once
#include <SDL/SDL.h>
#include <GL/glew.h>


enum class GameState { PLAY, EXIT };

class MainGame
{
public:
	MainGame();
	~MainGame();

	void run();



private:
	void initSystems();
	void gameLoop();
	void processInput();
	void drawGame();

	SDL_Window* _window;
	int _screenWidth;
	int _screenHeight;
	GameState _gameState;


};
//
//This is the code for the MainGame.cpp part of the class
#include "MainGame.h"
#include <iostream>


MainGame::MainGame() {
	_window = nullptr;
	_screenWidth = 824;
	_screenHeight = 686;
	_gameState = GameState::PLAY;
}

MainGame::~MainGame() {
}


void MainGame::run() {
	initSystems();


	gameLoop();
}

void MainGame::initSystems() {
	//Initialize SDL
	SDL_Init(SDL_INIT_EVERYTHING);

	_window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _screenWidth, _screenHeight, SDL_WINDOW_OPENGL);
	
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

	glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
}

void MainGame::gameLoop() {
	while (_gameState != GameState::EXIT) {
		processInput();
		drawGame();
	}
}


void MainGame::processInput() {
	SDL_Event evnt;

	while (SDL_PollEvent(&evnt)) {
		switch (evnt.type) {
		case SDL_QUIT:
			_gameState = GameState::EXIT;
			break;
		case SDL_MOUSEMOTION:
			std::cout << evnt.motion.x << " " << evnt.motion.y << std::endl;
			break;
		}
	}
}

void MainGame::drawGame() {
	glClearDepth(1.0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



	SDL_GL_SwapWindow(_window);
}
//
//This is the main.cpp code
#include "MainGame.h"
#include <GL/glew.h>


#include <iostream>

static unsigned int CompileShader(unsigned int type, const std::string& source) 
{
	unsigned int id = glCreateShader(type);
	const char* src = source.c_str();
	glShaderSource(id, 1, &src, nullptr);
	glCompileShader(id);
	
	return id;
}

static int CreateShader(const std::string& vertexShader, const std::string& fragmentShader) {
	GLuint program = glCreateProgram();
	GLuint vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
	GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);

	glAttachShader(program, vs);
	glAttachShader(program, fs);
	glLinkProgram(program);
	glValidateProgram(program);
	glDeleteShader(vs);
	glDeleteShader(fs);

	return program;
}


int main(int argc, char** argv) {
	MainGame mainGame;
	mainGame.run();
	
	//glewInit();
	//glewExperimental = GL_TRUE;

	float pos[6] = {
		-0.5f,-0.5f,
		 0.0f, 0.5f, 
		 0.5f,-0.5f};



	GLuint voID;
	glGenBuffers(1, &voID); //This is where the exception is thrown
	glBindBuffer(GL_ARRAY_BUFFER, voID);
	glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), pos, GL_STATIC_DRAW);


	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
	
	std::string vertexShader =
		"#version 330 core\n"
		"\n"
		"layout(location = 0) in vec4 position;\n"
		"\n"
		"void main()\n"
		"{\n"
		"gl_Position = position; \n"
		"}\n";


    std::string fragmentShader =
		"#version 330 core\n"
		"\n"
		"layout(location = 0) out vec4 color;"
		"\n"
		"void main()\n"
		"{\n"
		"color = vec4(1.0, 0.0, 0.0, 1.0); \n"
		"}\n";
		unsigned int shader = CreateShader(vertexShader, fragmentShader);
		glUseProgram(shader);
	
	
	glClear(GL_COLOR_BUFFER_BIT); 
	glDrawArrays(GL_TRIANGLES, 0, 3);







	return 0;
}

OpenGL doesn’t throw exceptions. You’re probably just crashing because you haven’t initialized things properly.

This is apparently the first GL call you’re making in your application. You can’t just start making GL calls. You have to create and bind an OpenGL context first.

You can:

  1. Do this manually if you want (using platform-specific APIs like wglCreateContext / glXCreateContext / etc.) along with using platform-specific API calls to create a window and target context rendering to the window, … or
  2. Just use a cross-platform library like GLFW or GLUT.
    This latter option is much easier when getting started because it’s just a few simple calls to get up-and-running, there’s lots of copy/paste code samples and docs for them, and they’re cross-platform.

Also consider using an extension loader like GLEW, as with just one call to the library, it provides all the supported OpenGL function pointers to you and provides for simple conditional tests on the presence of OpenGL extensions. See the Basic Instructions here, which has copy/paste code for using it with GLUT.

A few links:

First, you haven’t created and bound a context. You need to call SDL_GL_CreateContext and SDL_GL_MakeCurrent before calling any OpenGL functions.

Second, you shouldn’t be calling any OpenGL functions after receiving a SDL_QUIT event because the window may no longer exist at that point.