C++编译错误,构造函数没有返回类型...但我没有指定一个

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

本文关键字:一个 错误 编译 构造函数 C++ 返回类型      更新时间:2023-10-16

错误如下:1>c:usersbendocumentsvisual studio 2010projectsopengl_learningopengl_learning_without_glut OpenGLContext .cpp(18):错误C2533: 'OpenGLContext::{ctor}':构造函数不允许返回类型

这里是错误指向的代码块,特别是错误源自默认构造函数:

#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
}

为什么! ?

很可能您在OpenGLContext的定义后面忘记了一个分号。然后你的代码被解析为

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

在语法上是有效的。但是,由于构造函数没有返回类型,就像消息所说的那样,编译器会报错。

头文件中类定义后缺少分号

打开OpenGLContext.h文件,并确保在OpenGLContext类定义后放置分号

类定义的末尾应该有一个分号

相关文章: