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

发布于 2024-11-01 17:27:33 字数 1007 浏览 2 评论 0原文

所以这是错误: 1>c:\users\ben\documents\visual studio 2010\projects\opengl_learning\opengl_learning_without_glut\openglcontext.cpp(18): error 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
}

为什么!?

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!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倒数 2024-11-08 17:27:33

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

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

语法上有效。但由于构造函数没有返回类型,如消息所述,编译器会抱怨。

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.

清音悠歌 2024-11-08 17:27:33

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

Missing semicolon after the class definition in the header file

暗藏城府 2024-11-08 17:27:33

打开文件 OpenGLContext.h 并确保您是否在 OpenGLContext 类定义后面添加了分号。

Open the file OpenGLContext.h and make sure if you've put semicolon after OpenGLContext class definition.

弄潮 2024-11-08 17:27:33

类定义末尾应有一个分号。

The class definition should have a semicolon at the end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文