Hallo,
ich habe mir vorgenommen, dass ich mir ein kleines Programm-Kit zusammenbastel, damit nicht immer für jedes neue Projekt für kleinere Aufgaben relativ viel Zeit verschwenden muss. So habe ich eine kleine Klasse für das Erstellen und verwalten eines API-Fenster geschrieben.
Diese funktioniert prächtig, allerdings gibt es Probleme beim Vererben. Sobald ich die Klasse nämlich vererbt habe, spuckt mir der Compiler diese Fehlermeldung aus:
|
C-/C++-Quelltext
|
1
|
Unbehandelte Ausnahme bei 0x00000000 in directX_First.exe: 0xC0000005: Access violation.
|
. Ich schließe daraus, dass etwas mit meiner Speicherverwaltung nicht stimmt. Allerdings finde ich keinen Fehler.
Ich poste mal die Klasse (und kommentiere sie für euch aus)
Basisklasse:
Deklaration:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#pragma once
//-----------------------------------------------------------------//
// Includes
//-----------------------------------------------------------------//
#include <Windows.h>
#include <pkString.h> // weitere Klasse meines Programm-Kit's. (An dieser Klasse kann es aber nicht liegen (Denn auch ohne ihr erscheint der selbe Fehler
//-----------------------------------------------------------------//
// Klasse: pkWindow
//-----------------------------------------------------------------//
class pkWindow
{
public:
MSG msg;
//Konstruktor:
pkWindow();
pkWindow(int, int, pkString, WNDPROC);
pkWindow(int, int, WNDPROC);
pkWindow(pkString, WNDPROC);
~pkWindow();
bool Init();
bool Show();
virtual void WindowLoop();
protected:
WNDCLASSEX wndclass;
HWND hWnd;
HINSTANCE hInstance;
int height, width;
pkString title;
WNDPROC WinProc;
};
|
Definition:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
//-----------------------------------------------------------------//
// Includes
//-----------------------------------------------------------------//
#include <ProKit.h>
//-----------------------------------------------------------------//
// Klassendefinition: pkWindow
//-----------------------------------------------------------------//
pkWindow::pkWindow()
: width(50), height(50), title("pkWindow")
{}
pkWindow::pkWindow(int _width, int _height, pkString _title, WNDPROC _WinProc)
: width(_width), height(_height), title(_title)
{
WinProc = _WinProc;
}
pkWindow::pkWindow(int _width, int _height, WNDPROC _WinProc)
: width(_width), height(_height), title("PkWindow")
{
WinProc = _WinProc;
}
pkWindow::pkWindow(pkString _title, WNDPROC _WinProc)
: width(50), height(50), title(_title)
{
WinProc = _WinProc;
}
pkWindow::~pkWindow()
{}
bool pkWindow::Init()
{
//Fenstereigenschaften:
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WinProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
wndclass.lpszClassName = L"First API";
wndclass.lpszMenuName = 0;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wndclass))
return false;
return true;
}
bool pkWindow::Show()
{
if (!(hWnd = CreateWindowEx(NULL,
L"First API",
title.toLPCWSTR(),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0, 0,
width, height,
NULL, NULL,
hInstance,
NULL)))
return false;
return true;
}
void pkWindow::WindowLoop()
{/*Auskommentiert, da hier kein Fehlerrisiko besteht*/}
|
Unterklasse mit einer außerhalb der Klasse definierten Funktion:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
|
LRESULT __stdcall WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){/*Auskommentiert, da hier kein Fehlerrisiko besteht*/}
class GShell : public pkWindow
{
public:
GShell(int _width, int _height, pkString _title)
: pkWindow(_width, _height, _title, WinProc)
{}
};
|
Und schließlich das Programm:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//Includes
#include <Windows.h>
#include "InitWindow.h"
//Main-Funktion
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE preInstance, LPSTR lpCmdline, int command)
{
GShell win(800, 600, "GShell");
if (!win.Init())
return 1;
if(!win.Show()) //Hier müsste das Problem sein!!!
{
MessageBox(NULL, L"Fehler", L"Fehler", NULL);
return 1;
}
win.WindowLoop();
return 0;
}
|
Wie gesagt, bei direkter Instanziierung, also nicht über den Umweg der Abgeleiteten Klasse, gibt es keine Probleme.
Kann mir jemand weiterhelfen?