Anfängercode, bitte Fehler suchen
-
Ok, ich möchte die Bitch jetzt auch mal bezwingen und hab' mit der WinApi angefangen. Ich weiß, dass das nicht so lustig ist aber könntet ihr mir trotdem bei der Fehlersuche behilflich sein?
Entschuldigung, dass ich den ganzen Code poste aber ich weiß partout nicht wo der Fehler sein könnte.#define STRICT #include <windows.h> class Pix { public: Pix(int InitialX, int InitialY); ~Pix(); int GetX(); int GetY(); void SetX(int x); void SetY(int y); private: int PosX; int PosY; }; Pix::Pix(int InitialX, int InitialY) { PosX = InitialX; PosY = InitialY; } Pix::~Pix() { } int Pix::GetX() { return PosX; } int Pix::GetY() { return PosY; } void Pix::SetX(int x) { PosX = x; } void Pix::SetY(int y) { PosY = y; } LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); const char szAppName[] = "Verschiebung eines Pixels"; int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MSG msg; HWND hWnd; WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wc.lpszClassName = szAppName; wc.lpszMenuName = NULL; RegisterClass(&wc); hWnd = CreateWindow( szAppName, szAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, iCmdShow); UpdateWindow(hWnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { Pix One(50,50); HDC hdc ; PAINTSTRUCT ps ; HPEN hPen; switch (message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); SelectObject(hdc, GetStockObject(WHITE_BRUSH)); int x = One.GetX(); int y = One.GetY() SetPixel(hdc,x,y,RGB(255,0,0)); DeleteObject(hPen); EndPaint(hwnd, &ps); return 0 ; case WM_KEYDOWN: switch(wParam) { case VK_UP: int i = One.GetY(); i++; One.SetY(i) break; case VK_DOWN: int j = One.GetY(); j--; One.SetY(j); break; } return 0; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }
Und hier die Compilerfehler (Dev-C++) aus denen ich leider auch nicht so rihtig schlau werde...
c:\c++\dev-c++\bin\textausgabe 1.0.cpp: In function `LRESULT WndProc(HWND__ *, unsigned int, unsigned int, long int)': c:\c++\dev-c++\bin\textausgabe 1.0.cpp:114: parse error before `(' c:\c++\dev-c++\bin\textausgabe 1.0.cpp:120: jump to case label c:\c++\dev-c++\bin\textausgabe 1.0.cpp:112: crosses initialization of `int y' c:\c++\dev-c++\bin\textausgabe 1.0.cpp:111: crosses initialization of `int x' c:\c++\dev-c++\bin\textausgabe 1.0.cpp:129: jump to case label c:\c++\dev-c++\bin\textausgabe 1.0.cpp:124: crosses initialization of `int i' c:\c++\dev-c++\bin\textausgabe 1.0.cpp:137: jump to case label c:\c++\dev-c++\bin\textausgabe 1.0.cpp:112: crosses initialization of `int y' c:\c++\dev-c++\bin\textausgabe 1.0.cpp:111: crosses initialization of `int x'
Ich hoffe ihr könnt mir helfen, danke schoneinmal im voraus
-
Moin, Moin
Folgende Fehler habe ich gefunden:
- Fehlendes Semikolon in 'int y = One.GetY()' und in 'One.SetY(i)'
- Die Variablen x,y und i in WndProc solltest Du am Anfang der Prozedur deklarieren.
#define STRICT #include <windows.h> class Pix { public: Pix(int InitialX, int InitialY); ~Pix(); int GetX(); int GetY(); void SetX(int x); void SetY(int y); private: int PosX; int PosY; }; Pix::Pix(int InitialX, int InitialY) { PosX = InitialX; PosY = InitialY; } Pix::~Pix() { } int Pix::GetX() { return PosX; } int Pix::GetY() { return PosY; } void Pix::SetX(int x) { PosX = x; } void Pix::SetY(int y) { PosY = y; } LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); const char szAppName[] = "Verschiebung eines Pixels"; int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MSG msg; HWND hWnd; WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wc.lpszClassName = szAppName; wc.lpszMenuName = NULL; RegisterClass(&wc); hWnd = CreateWindow( szAppName, szAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, iCmdShow); UpdateWindow(hWnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { Pix One(50,50); HDC hdc ; PAINTSTRUCT ps ; HPEN hPen; int x, y, i, j; switch (message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); SelectObject(hdc, GetStockObject(WHITE_BRUSH)); x = One.GetX(); y = One.GetY(); SetPixel(hdc,x,y,RGB(255,0,0)); DeleteObject(hPen); EndPaint(hwnd, &ps); return 0 ; case WM_KEYDOWN: switch(wParam) { case VK_UP: i = One.GetY(); i++; One.SetY(i); break; case VK_DOWN: j = One.GetY(); j--; One.SetY(j); break; } return 0; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }
So müsste es übersetzt werden. Es gibt noch eine Warnung, dass hPen benutzt wird ohne initialisiert worden zu sein.
Ciao...
[ Dieser Beitrag wurde am 09.04.2003 um 14:20 Uhr von Kal El editiert. ]
-
wenn du neue variable im switch/case erstellst muss da ein block { } rum und dann hast du ein 2 stellen das ; vergessen
-
Ok, ich habe jetzt nach längerem Basteln keine Compilerfehler mehr aber dafür einen Laufzeitfehler. In der Art, dass sich der Pixel, den ich verschieben möchte, nicht bewegt. Könnt ihr mir weiterhelfen?
#define STRICT #include <windows.h> #include "Assert.cpp" #define DEBUG class Pix { public: Pix(int InitialX, int InitialY); ~Pix(); int GetX(); int GetY(); void SetX(int); void SetY(int); private: int PosX; int PosY; }; Pix::Pix(int InitialX, int InitialY) { PosX = InitialX; PosY = InitialY; } Pix::~Pix() { } int Pix::GetX() { return PosX; } int Pix::GetY() { return PosY; } void Pix::SetX(int x) { PosX = x; } void Pix::SetY(int y) { PosY = y; } LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); const char szAppName[] = "Verschiebung eines Pixels"; int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MSG msg; HWND hWnd; WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wc.lpszClassName = szAppName; wc.lpszMenuName = NULL; RegisterClass(&wc); hWnd = CreateWindow( szAppName, szAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, iCmdShow); UpdateWindow(hWnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { Pix One(50,50); HDC hdc ; PAINTSTRUCT ps ; HPEN hPen; int x, y; switch (message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); SelectObject(hdc, GetStockObject(WHITE_BRUSH)); SetPixel(hdc,One.GetX(),One.GetY(),RGB(255,0,0)); EndPaint(hwnd, &ps); return 0 ; case WM_KEYDOWN: switch(wParam) { case VK_UP: y = One.GetY(); y++; One.SetY(y); InvalidateRect(hwnd, NULL, TRUE); ASSERT (One.GetY() == 51); break; case VK_DOWN: y = One.GetY(); y--; One.SetY(y); InvalidateRect(hwnd, NULL, TRUE); ASSERT (One.GetY() == 49); break; case VK_LEFT: x = One.GetX(); x--; One.SetX(x); InvalidateRect(hwnd, NULL, TRUE); ASSERT (One.GetX() == 49); break; case VK_RIGHT: x = One.GetX(); x++; One.SetX(x); InvalidateRect(hwnd, NULL, TRUE); ASSERT (One.GetX() == 51); break; default: return 0; } return 0; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }
-
Hallo Sp*****wein!
Dein Pixel bewegt sich deswegen nicht, weil Pix One eine lokale Variable ist, die Jedes mal wenn deine Funktion aufgerufen wird neu erstellt wird!
Du müsstest Pix One als static declarieren:
static Pix One(50, 50)
-
*****
-
Ich weis nicht wie diese Sternchen dahinkamen?
Das ist schon einemal vor einer Woche so gewesen!Hallo Sp*****wein
-
Ich habe habs:
Das Forum ersetz ein Paar Schimpfwörter durch diese Sternchen.
Dazu gehört eben auch das "Hinterteil". Und da der Name S-p-a-r-s-c-h-w-e-i-n das Wort A-R-S-C-H enthält wird es durch die Sternchen ersetzt.
-
Du bist ein echter Blitzmerker, du A-r-s-c-h-l-o-c-h!
-
Begründe einmal deine letze Aussage WebFritzi!