Skip to main content
GameDev.net gamedev.net
🔒 Locked

Derived classes and base methodes ( C++)

Started by guppy Jan 5, 2003 at 5:42 AM 5 replies 400+ views
Original Post
guppy
guppy
[edit] Thes Question has been answeared, and realy has no informative value as the problem was a "typo" [/edit] Im having a bit of a problem understanding why this does not work: Methode in the derived class:
    
void WindowObject::Create( HWND parent = 0)
{
  
HICON   hIcon;   //window icon

HCURSOR hCursor; //window cursor


this->windowClass ="test";
WNDCLASSEX wcx;    
hIcon=(HICON)LoadImage(0,IDI_APPLICATION,IMAGE_ICON,0,0,LR_SHARED);
hCursor=(HCURSOR)LoadImage(0,IDC_ARROW,IMAGE_CURSOR,0,0,LR_SHARED);
wcx.cbSize           = sizeof(WNDCLASSEX);              //byte size of WNDCLASSEX struct

wcx.style            = CS_HREDRAW|CS_VREDRAW;           //ensure wnd is always redrawn

wcx.lpfnWndProc      = (WNDPROC)WndProc;                //pointer to the Window Procedure

wcx.cbClsExtra       = 0;                               //Extra bytes for 'class' wnds

wcx.cbWndExtra       = 0;                               //Extra bytes for this wnd

wcx.hInstance        = this->aplication;                       //Application instance

wcx.hIcon            = hIcon;                           //Application icon

wcx.hCursor          = hCursor;                         //Cursor for wnd

wcx.hbrBackground    = (HBRUSH)(COLOR_BTNFACE+1);       //Background wnd colour

wcx.lpszMenuName     = NULL;                            //Name of wnd menu

wcx.lpszClassName    = this->windowClass.data();
wcx.hIconSm          = NULL;                            //Icon in top-left corner of wnd

//Register the wnd class with the Windows system

if (!RegisterClassEx(&wcx))
    {
    //Registration has failed so inform the user

    MessageBox( NULL,
                TEXT("Failed to register wnd class"),
                TEXT("ERROR"),
                MB_OK|MB_ICONERROR);
        fail=true;
        errorMsg = "";
    }
    else
    {
        this->windowStyle=WS_OVERLAPPEDWINDOW;
     GUIObject::Create(parent);
    }
}
  

The same methode in the class that the above derives from
      
 void GUIObject::Create( HWND parent = 0)
 {
 this->parent = parent;
 this->owner=CreateWindowEx(
                    this->windowExStyle,
                    this->windowClass.data(),
                    this->caption.data(),
                    this->windowStyle,
                    this->posX,
                    this->posY,
                    this->width,
                    this->height,
                    this->parent,
                    (HMENU)this->ID,
                    this->aplication,
                    reinterpret_cast<void*>(this));     //Store the object pointer for later use..

if (!this->owner)
    {
    //wnd creation has failed so inform the user  

    MessageBox( NULL,
                TEXT("Failed to create window"),
                TEXT("ERROR"), 
                MB_OK|MB_ICONERROR);
     this->fail=true;
     this->errorMsg="Failed to create window";
    }
    else
    {
     this->created=true;
    }
 }
    
I get the messagebox saying "failed to create window" but if I simply copies the code from the base class into the derived one instead of calling everything works fine... any thougths? ( It may be that the answer is very simple, at this point im fairly sure that i've stared myself blind on this code ) ps. Forum maintainers: I't would be nice to be able to preview ones post's... just a thougth.. /Please excuse my bad spelling - My native language is binary not english |Visit me \Take my advice - I don''t use it... [edited by - guppy on January 5, 2003 9:03:08 AM]
/Please excuse my bad spelling - My native language is binary not english|Visit meTake my advice - I don''t use it...
Fruny
Fruny
show your class declarations (member variables, whether Create is virtual...)


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
guppy
guppy
quote:
Original post by Fruny
show your class declarations (member variables, whether Create is virtual...)


well you asked for it (btw, nice response time :D)

& yes Create is virtual...

base class:

  
class GUIObject
{
private:
//This section should not be used as the class is abstract!


protected:
bool visible;
int posX, posY;
int height, width;
string caption;
string windowClass;
int windowExStyle;
int ID; //Controll ID or handle to menu

int windowStyle;
HWND owner; //Handle to the owning window or controll

HWND parent;
HINSTANCE aplication;


bool created; //This information may be need by the destructor.



virtual void updateVisibility() const = 0;

public:
bool fail;
string errorMsg;


virtual bool isVisible () const { return visible; }
virtual void show ( bool visible = true ) { this->visible = visible; updateVisibility();}
virtual void hide () { this->visible = false; updateVisibility();}
virtual bool setPosition ( int X, int Y ) { posX = X; posY = Y;}
virtual bool setSize( int Height, int Width ) {height = Height; width = Width;}
virtual void setInstance ( HINSTANCE App ) { aplication = App; }
virtual void update() const = 0; // used to force an update

// constructor is used only for initialsation the "Create" methode actualy creates the window or controll!!

virtual void Create ( HWND parent = 0 );
GUIObject();
virtual ~GUIObject() {}

};


Derived class:

  
class WindowObject : public GUIObject
{
//again just to ease typing...

typedef std::map<long, OOEventHandler*> EventHandlerMap;
typedef EventHandlerMap::iterator EventHandlerIterator;

private:
bool quitOnClose;
string windowClass; //should be done using a adv_windowClass class instead!! TODO 7

uint ocapacity;
EventHandlerMap eventHandlers;


virtual void updateVisibility() const;

public:
/*------- begin setup methodes -------*/
virtual void setTitle ( string title ) { caption = title; }
virtual void setWindowClass ( string windowClass ) { this->windowClass = windowClass; } //TODO 10
virtual bool setOcapacity ( uint percent ) { ocapacity = percent; } //TODO 7
virtual bool setQuitOnClose ( bool value ) { quitOnClose = value; }
/*------- end of setup methodes -------*/


virtual void update() const;
virtual void Create ( HWND parent = 0 );
WindowObject ();

//------ The message handler callback wrapper ------->

static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK OOWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);//TODO 1

};


btw, don''t mind the comments



/Please excuse my bad spelling - My native language is binary not english
|Visit me
\Take my advice - I don''''t use it...
/Please excuse my bad spelling - My native language is binary not english|Visit meTake my advice - I don''t use it...
Fruny
Fruny
Ah, yes, I think I found it. You use std::string::data(), which is not NULL-terminated, in several places where I *think* the Win32 API requires a NULL-terminated string - try std::string::c_str() instead. Additionally, I do hope the API doesn''t just keep a pointer to it, but does make a copy...


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
guppy
guppy
Hrmm.. didn't see the distiction - will try it now
[edit]Good advice, sadly that was not what caused it [/edit]
/Please excuse my bad spelling - My native language is binary not english
|Visit me
\Take my advice - I don''t use it...

[edited by - guppy on January 5, 2003 7:16:28 AM]
/Please excuse my bad spelling - My native language is binary not english|Visit meTake my advice - I don''t use it...
Fruny
Fruny
Have you replaced them all ?

Try checking the error code returned by GetLastError to see the nature of the error... the window class name "test" might already be taken

Beyond that, I don''t really know, I''m not a Win32 expert.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
guppy
guppy
The problem seems to be that this->windowClass is empthy when viewed from the base methode... all other attributes have their proper values, and the only thing that makes windowClass stand out is that it is set in the calling methode... no idea why this is so - but hey atlest now I can fix it

[edit]
Oh well - it was a bit more special than that... heh seems I redefined windowClass in the derived class... UPS.. :S
[/edit]

/Please excuse my bad spelling - My native language is binary not english
|Visit me
\Take my advice - I don''t use it...

[edited by - guppy on January 5, 2003 9:00:27 AM]
/Please excuse my bad spelling - My native language is binary not english|Visit meTake my advice - I don''t use it...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.