1

Here is my header

#pragma once
#ifndef BASE_H
#define BASE_H

#include <Windows.h>
#include <windowsx.h>

class Base
{
    HWND hWnd;
    WNDCLASSEX WndCls;
    HRESULT Hr;
public:
    Base();
    static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    void RegisterWnd(HINSTANCE hInstance);
    void ShowWnd(int nCmdShow);
    ~Base();
};

#endif

Here is my base.cpp

#include "Base.h"


Base::Base()
{
}

static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    // sort through and find what code to run for the message given
    switch (message)
    {
        // this message is read when the window is closed
    case WM_DESTROY:
    {
        // close the application entirely
        PostQuitMessage(0);
        return 0;
    } break;
    }

    // Handle any messages the switch statement didn't
    return DefWindowProc(hWnd, message, wParam, lParam);
}

void Base::RegisterWnd(HINSTANCE hInstance)
{
    ZeroMemory(&WndCls, sizeof(WNDCLASSEX));
    WndCls.cbSize = sizeof(WNDCLASSEX);
    WndCls.hbrBackground = (HBRUSH)COLOR_WINDOW;
    WndCls.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndCls.hIcon = LoadIcon(hInstance, NULL);
    WndCls.hIconSm = LoadIcon(hInstance, NULL);
    WndCls.hInstance = hInstance;
    WndCls.lpfnWndProc = WndProc;
    WndCls.lpszClassName = "ClsName";
    WndCls.style = CS_HREDRAW | CS_VREDRAW;

    Hr = RegisterClassEx(&WndCls);
    if (FAILED(Hr))
        MessageBox(NULL, "Window Class failed to register.", "ERROR", MB_OK);

    hWnd = CreateWindowEx(
        NULL,
        "WndClassName",
        "WndName",
        WS_OVERLAPPEDWINDOW,
        100, 100,
        480, 640,
        NULL,
        NULL,
        hInstance,
        NULL);
    if (FAILED(hWnd))
        MessageBox(NULL, "Window Class failed to create", "ERROR", MB_OK);
}

void Base::ShowWnd(int nCmdShow)
{
    Hr = ShowWindow(hWnd, nCmdShow);
    if (FAILED(Hr))
        MessageBox(NULL, "Failed to display Window", "ERROR", MB_OK);
}

Base::~Base()
{
}

And here is my main.cpp

#include "Base.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    Base CreateWnd;

    CreateWnd.RegisterWnd(hInstance);
    CreateWnd.ShowWnd(nCmdShow);

    MSG Msg;
    while (GetMessage(&Msg, NULL, 0, 0))
    {
        // translate keystroke messages into the right format
        TranslateMessage(&Msg);

        // send the message to the WindowProc function
        DispatchMessage(&Msg);
    }

    // return this part of the WM_QUIT message to Windows
    return Msg.wParam;
}

The problem is, I keep getting this error message that I dont understand of. Sorry for the bad explanation..Still a student in programming... Here is the image of the error

UPDATED : The error above has been corrected by replacing

static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

with

LRESULT CALLBACK Base::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

Thanks to ravi and IInspectable for the quick help.

Now I am having another error D: When i clicked on debug, everything run perfectly but nothing shows up. No window is showing. Visual studio is running perfectly as "Ready". (Sorry i do not want to make another new question because it's still related to creating window in oo

SECOND UPDATE : My class name in CreateWindowEx is different from the RegisterWnd..My bad. Thanks to IInspectable again for the help.

Josh Lcs
  • 55
  • 5
  • 2
    @πάντα ῥεῖ: Sorry, but this is definitely not a duplicate. Either you didn't understand the problem of the question, or you're just lazy. The root of the problem is that a class member method (symbol) `WndProc` is declared and used in the `WNDCLASSEX` structure, but that the actual implementation of `WndProc` is (of course) not a class method (because the expected prototype for Win32 window procedures is not fit for class member functions). However the class member symbol overlays the global scope symbol. You could either get rid of the class member declaration or use the `::` scope operator. – datenwolf Nov 08 '14 at 11:05
  • @πάντα ῥεῖ: So, yeah, thanks for closing "duplicate" (which it is not) so that people can not really give proper answers. Well done… well done. – datenwolf Nov 08 '14 at 11:06

1 Answers1

2
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

You have to define this with class scope OR how compiler know if its global static OR static member of class. So it should be

LRESULT CALLBACK Base::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
ravi
  • 10,994
  • 1
  • 18
  • 36
  • 1
    You cannot use the `static` keyword when defining the static class member. The correct implementation is: `LRESULT CALLBACK Base::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { ... }`. – IInspectable Nov 08 '14 at 11:01
  • But then the static will have an error in the cpp: "storage class may not be specified here" – Josh Lcs Nov 08 '14 at 11:02
  • Thanks alot. @IInspectable! But when i clicked on debug, there was no error but no window is showing up as well. Visual studio is "ready" but nothing shows up. Any idea? – Josh Lcs Nov 08 '14 at 11:05
  • 2
    @Josh: You are trying to create a class with a **window class name** you haven't registered. Your call to `CreateWindowEx` should use the same name (`"ClsName"`) that was used in the call to `RegisterClassEx`. Also, the `FAILED` macro is for use with COM **only**. Windows API calls report failure as outlined in the respective documentation. However, please open a new question. Your initial question has been addressed. – IInspectable Nov 08 '14 at 11:17
  • @IInspectable Oh. So dumb of me changing the class name. Once again thanks so much for the help! Work like a charm now :D – Josh Lcs Nov 08 '14 at 11:20