C++编译错误,构造函数没有返回类型...但我没有指定返回类型
所以这是错误: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您很可能忘记了 OpenGLContext 定义后的分号。然后你的代码被解析为
语法上有效。但由于构造函数没有返回类型,如消息所述,编译器会抱怨。
Most likely you forgot a semicolon after
OpenGLContext
's definition. Then your code is parsed asThat's valid syntactically. But as constructors don't have a return type, like the message says, the compiler complains.
头文件中的类定义后面缺少分号
Missing semicolon after the class definition in the header file
打开文件
OpenGLContext.h
并确保您是否在OpenGLContext
类定义后面添加了分号。Open the file
OpenGLContext.h
and make sure if you've put semicolon afterOpenGLContext
class definition.类定义末尾应有一个分号。
The class definition should have a semicolon at the end.