Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ compilation error, constructor has no return type... but I didn't specify one

So here is the error: 1>c:\users\ben\documents\visual studio 2010\projects\opengl_learning\opengl_learning_without_glut\openglcontext.cpp(18): error C2533: 'OpenGLContext::{ctor}' : constructors not allowed a return type

And here is a block of code where the error points, specifically the error originates from the default constructor:

#include <Windows.h>
#include <iostream>
#include "OpenGLContext.h"


/**
    Default constructor for the OpenGLContext class. At this stage it does nothing 
    but you can put anything you want here. 
*/
OpenGLContext::OpenGLContext(void){}
OpenGLContext::OpenGLContext(HWND hwnd) { 
    createContext(hwnd); 
}
/** 
    Destructor for our OpenGLContext class which will clean up our rendering context 
    and release the device context from the current window. 
*/  

OpenGLContext::~OpenGLContext(void) { 
    wglMakeCurrent(hdc, 0); // Remove the rendering context from our device context
    wglDeleteContext(hrc); // Delete our rendering context 
    ReleaseDC(hwnd, hdc); // Release the device context from our window
}

Why!?

like image 971
acp10bda Avatar asked Apr 18 '11 11:04

acp10bda


1 Answers

Most likely you forgot a semicolon after OpenGLContext's definition. Then your code is parsed as

class OpenGLContext { /* ... */ } OpenGLContext::OpenGLContext(void) { }

That's valid syntactically. But as constructors don't have a return type, like the message says, the compiler complains.

like image 181
Johannes Schaub - litb Avatar answered Oct 05 '22 23:10

Johannes Schaub - litb