4 Replies - 273 Views - Last Post: 07 June 2011 - 12:54 PM Rate Topic: -----

#1 JL29  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 20-May 10

Why does calling this member of a class not work?

Posted 07 June 2011 - 12:32 PM

Hello,

I am learning c++ but stumbled upon a problem. I have 2 files, INC.h and Main.cpp. Here are the codes:
INC.h:
#include <Windows.h>

class Window {
	private:
	DWORD _STYLE;

	public:
		HWND NewWindow(LPCTSTR _TITLE,int _X, int _Y,int _WIDTH, int _HEIGHT,HINSTANCE hInstance, WNDPROC _WINPROC,bool _WINDOWED);
};


Main.cpp:
#include "INC.h"

HWND Window::NewWindow(LPCTSTR _TITLE,int _X, int _Y,int _WIDTH, int _HEIGHT,HINSTANCE hInstance, WNDPROC _WINPROC,bool _WINDOWED) {
			WNDCLASSEX _CLSTRUCT;

			ZeroMemory(&_CLSTRUCT,sizeof(_CLSTRUCT));

			_CLSTRUCT.cbSize = sizeof(_CLSTRUCT);
			_CLSTRUCT.hCursor = LoadCursor(NULL,IDC_ARROW);
			_CLSTRUCT.hInstance = hInstance;
			_CLSTRUCT.lpszClassName = "WCLASS";
			_CLSTRUCT.lpfnWndProc = _WINPROC;
			_CLSTRUCT.style = NULL;

			RegisterClassEx(&_CLSTRUCT);

			if(_WINDOWED)
			{ _STYLE = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE; }
			else if (!_WINDOWED)
			{ _STYLE = WS_POPUP; }

			return CreateWindowEx(	NULL,
									"WCLASS",
									_TITLE,
									_STYLE,
									_X,
									_Y,
									_WIDTH,
									_HEIGHT,
									NULL,
									NULL,
									hInstance,
									NULL );
}

LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM par1, LPARAM par2) {
}

int WINAPI WinMain() {
	HWND WINDOW;
	return 0;
}



But as soon as I try to change HWND WINDOW; to HWND WINDOW = NewWindow(... VC2010 says that NewWindow is not defined, why? How do I solve this?

EDIT: This is not from a tutorial, I am trying to create this myself without tutorials.

Thanks in advance,

JL29.

This post has been edited by JL29: 07 June 2011 - 12:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Why does calling this member of a class not work?

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Why does calling this member of a class not work?

Posted 07 June 2011 - 12:37 PM

Because you haven't created an instance of your class Window

    Window wndw;
    WINDOW = wndw.NewWindow(//blah...);


Was This Post Helpful? 1
  • +
  • -

#3 JL29  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 20-May 10

Re: Why does calling this member of a class not work?

Posted 07 June 2011 - 12:39 PM

View PostButchDean, on 07 June 2011 - 12:37 PM, said:

Because you haven't created an instance of your class Window

    Window wndw;
    WINDOW = wndw.NewWindow(//blah...);



Oh great, my first beginner mistake. :stupid:
Thanks for the help, I don't think I will forget this next time :D
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Why does calling this member of a class not work?

Posted 07 June 2011 - 12:47 PM

View PostJL29, on 07 June 2011 - 08:39 PM, said:

View PostButchDean, on 07 June 2011 - 12:37 PM, said:

Because you haven't created an instance of your class Window

    Window wndw;
    WINDOW = wndw.NewWindow(//blah...);



Oh great, my first beginner mistake. :stupid:
Thanks for the help, I don't think I will forget this next time :D

No problem. Have fun coding! :)
Was This Post Helpful? 0
  • +
  • -

#5 JL29  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 20-May 10

Re: Why does calling this member of a class not work?

Posted 07 June 2011 - 12:54 PM

Thanks!

And for the curious, here is the finished Main.cpp:
#include "INC.h"

Window Windows;
#define HEIGHT 600
#define WIDTH 800
#define WINDOWED true

HWND Window::NewWindow(LPCTSTR _TITLE,int _X, int _Y,int _WIDTH, int _HEIGHT,HINSTANCE hInstance, WNDPROC _WINPROC,bool _WINDOWED) {
			WNDCLASSEX _CLSTRUCT;

			ZeroMemory(&_CLSTRUCT,sizeof(_CLSTRUCT));

			_CLSTRUCT.cbSize = sizeof(_CLSTRUCT);
			_CLSTRUCT.hCursor = LoadCursor(NULL,IDC_ARROW);
			_CLSTRUCT.hInstance = hInstance;
			_CLSTRUCT.lpszClassName = "WCLASS";
			_CLSTRUCT.lpfnWndProc = _WINPROC;
			_CLSTRUCT.style = NULL;

			RegisterClassEx(&_CLSTRUCT);

			if(_WINDOWED)
			{ _STYLE = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE; }
			else if (!_WINDOWED)
			{ _STYLE = WS_POPUP; }

			return CreateWindowEx(	NULL,
									"WCLASS",
									_TITLE,
									_STYLE,
									_X,
									_Y,
									_WIDTH,
									_HEIGHT,
									NULL,
									NULL,
									hInstance,
									NULL );
}

LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM par1, LPARAM par2) {
	switch(msg)
	{
		case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
		break;
	}

	return DefWindowProc(hWnd,msg,par1,par2);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR cmdLine,int noCmd) {
	MSG msg;

	HWND WINDOW = Windows.NewWindow("WINDOW",0,0,WIDTH,HEIGHT,hInstance,WinProc,WINDOWED);

	while(TRUE)
	{
		PeekMessage(&msg,WINDOW,0,0,PM_REMOVE);
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		if(msg.message == WM_QUIT){
			break;
		}
	}
	return 0;
}



I dont know id I did something stupid or something, but it works! :D It's kinda hard to memorize all these things when learning it. But 90% of this is without looking at a tutorial or previous program :D (Learned this in... 4 days with c# experience)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1