【发布时间】:2015-03-23 23:49:54
【问题描述】:
我是 C++ 的新手,但有一些高级语言(Java 等)的编程经验。我目前面临的主要问题是理解C++中的继承和接口。
testsurface.h
#include <QSurface>
class TestSurface : public virtual QSurface
{
public:
TestSurface(SurfaceClass clazz) : QSurface(clazz) { }
~TestSurface() { }
virtual QSurfaceFormat format() = 0;
virtual QSize size() = 0;
virtual QPlatformSurface* surfaceHandle() = 0;
virtual SurfaceType surfaceType() = 0;
};
testwindow.h
#include <QWindow>
#include "testsurface.h"
class TestWindow : public QWindow, public TestSurface
{
public:
TestWindow() : QWindow(), TestSurface(QWindow::Window) { }
~TestWindow() { }
};
在我的理解中,TestSurface 传递了来自QSurface 的所有抽象方法。因为TestWindow 继承QWindow 继承QSurface 我希望TestSurface::QSurface 的所有需要的方法都是通过TestWindow::QWindow 实现的,但事实并非如此,因为我在构建时收到以下错误消息:
C2512: C:\...\Qt\Projects\Test2\testwindow.h:10: Erroe: C2512: 'QSurface::QSurface': no appropriate default constructor available
和
C:\...\Qt\Projects\Test2\main.cpp:12: Error: C2259: 'TestWindow': Instance of abstract class can't be created because of following members:
"QSurfaceFormat TestSurface::format(void)": is abstract
c:\...\qt\projects\test2\testsurface.h(12): See declaration of 'TestSurface::format'
"QSize TestSurface::size(void)": is abstract
c:\...\qt\projects\test2\testsurface.h(13): See declaration of 'TestSurface::size'
"QPlatformSurface *TestSurface::surfaceHandle(void)": is abstract
c:\...\qt\projects\test2\testsurface.h(14): See declaration of 'TestSurface::surfaceHandle'
"QSurface::SurfaceType TestSurface::surfaceType(void)": is abstract
c:\...\qt\projects\test2\testsurface.h(15): See declaration of 'TestSurface::surfaceType'
"QSurfaceFormat QSurface::format(void) const": is abstract
D:\...\msvc2013_64_opengl\include\QtGui/qsurface.h(67): See declaration of 'QSurface::format'
"QPlatformSurface *QSurface::surfaceHandle(void) const": is abstract
D:\...\msvc2013_64_opengl\include\QtGui/qsurface.h(68): See declaration of 'QSurface::surfaceHandle'
"QSurface::SurfaceType QSurface::surfaceType(void) const": is abstract
D:\...\msvc2013_64_opengl\include\QtGui/qsurface.h(70): See declaration of 'QSurface::surfaceType'
"QSize QSurface::size(void) const": is abstract
D:\...\msvc2013_64_opengl\include\QtGui/qsurface.h(73): See declaration of 'QSurface::size'
【问题讨论】:
标签: c++ qt inheritance