www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Accessing class member methods

reply NewtoD <NewtoD_member pathlink.com> writes:
I get no compilation errors but when I debug, I get an access violation when the
call to a class member's method.  I compile this in visual studio .net 2003 with
the 3 files (this not an exact replica of the actual code) and the custom build
steps to link them together.  Then I use the vs debugger.  I thought this was
legal. :(

// main.d
import app;

int main(char[][] args)
{

app.App myapp = new app.App();
myapp.wnd.MsgBox("Test","Test", MB_OK);  // This line gets the violation

return 0;
}


//////////
// App.d

module app;

import window;

class App
{
public:
Window wnd;
}


////////////
// Window.d

module window;

import std.c.windows.windows;

class Window
{
public:
HANDLE hwnd;

void MsgBox(LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
{
MessageBoxA(null, lpText, lpCaption, uType);
return;
}
}
Mar 20 2005
parent reply "Alex Stevenson" <ans104 cs.york.ac.uk> writes:
Hi,

The problem here is that in App.d you've declared an instance of the  
Window class, but haven't initialised it. You need to write a constructor  
for App which creates a new Window:

 //////////
 // App.d

 module app;

 import window;

 class App
 {
 public:
 Window wnd;
+ this() + { + wnd = new Window(); + }
 }
On Sun, 20 Mar 2005 17:27:29 +0000 (UTC), NewtoD <NewtoD_member pathlink.com> wrote:
 I get no compilation errors but when I debug, I get an access violation  
 when the
 call to a class member's method.  I compile this in visual studio .net  
 2003 with
 the 3 files (this not an exact replica of the actual code) and the  
 custom build
 steps to link them together.  Then I use the vs debugger.  I thought  
 this was
 legal. :(

 // main.d
 import app;

 int main(char[][] args)
 {

 app.App myapp = new app.App();
 myapp.wnd.MsgBox("Test","Test", MB_OK);  // This line gets the violation

 return 0;
 }


 //////////
 // App.d

 module app;

 import window;

 class App
 {
 public:
 Window wnd;
 }


 ////////////
 // Window.d

 module window;

 import std.c.windows.windows;

 class Window
 {
 public:
 HANDLE hwnd;

 void MsgBox(LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
 {
 MessageBoxA(null, lpText, lpCaption, uType);
 return;
 }
 }
-- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Mar 20 2005
next sibling parent NewtoD <NewtoD_member pathlink.com> writes:
Thank you so much!  That did it.  So unlike c++, you must use the new keyword to
initialize an instance whether its a pointer or not?

In article <opsnx6jhgd08qma6 mjolnir.spamnet.local>, Alex Stevenson says...
Hi,

The problem here is that in App.d you've declared an instance of the  
Window class, but haven't initialised it. You need to write a constructor  
for App which creates a new Window:

 //////////
 // App.d

 module app;

 import window;

 class App
 {
 public:
 Window wnd;
+ this() + { + wnd = new Window(); + }
 }
On Sun, 20 Mar 2005 17:27:29 +0000 (UTC), NewtoD <NewtoD_member pathlink.com> wrote:
 I get no compilation errors but when I debug, I get an access violation  
 when the
 call to a class member's method.  I compile this in visual studio .net  
 2003 with
 the 3 files (this not an exact replica of the actual code) and the  
 custom build
 steps to link them together.  Then I use the vs debugger.  I thought  
 this was
 legal. :(

 // main.d
 import app;

 int main(char[][] args)
 {

 app.App myapp = new app.App();
 myapp.wnd.MsgBox("Test","Test", MB_OK);  // This line gets the violation

 return 0;
 }


 //////////
 // App.d

 module app;

 import window;

 class App
 {
 public:
 Window wnd;
 }


 ////////////
 // Window.d

 module window;

 import std.c.windows.windows;

 class Window
 {
 public:
 HANDLE hwnd;

 void MsgBox(LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
 {
 MessageBoxA(null, lpText, lpCaption, uType);
 return;
 }
 }
-- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Mar 20 2005
prev sibling parent reply NewtoD <NewtoD_member pathlink.com> writes:
Thank you so much, that did it!  Basically, all instances must be initialized
with the new keyword, pointer or not?
:)


In article <opsnx6jhgd08qma6 mjolnir.spamnet.local>, Alex Stevenson says...
Hi,

The problem here is that in App.d you've declared an instance of the  
Window class, but haven't initialised it. You need to write a constructor  
for App which creates a new Window:

 //////////
 // App.d

 module app;

 import window;

 class App
 {
 public:
 Window wnd;
+ this() + { + wnd = new Window(); + }
 }
On Sun, 20 Mar 2005 17:27:29 +0000 (UTC), NewtoD <NewtoD_member pathlink.com> wrote:
 I get no compilation errors but when I debug, I get an access violation  
 when the
 call to a class member's method.  I compile this in visual studio .net  
 2003 with
 the 3 files (this not an exact replica of the actual code) and the  
 custom build
 steps to link them together.  Then I use the vs debugger.  I thought  
 this was
 legal. :(

 // main.d
 import app;

 int main(char[][] args)
 {

 app.App myapp = new app.App();
 myapp.wnd.MsgBox("Test","Test", MB_OK);  // This line gets the violation

 return 0;
 }


 //////////
 // App.d

 module app;

 import window;

 class App
 {
 public:
 Window wnd;
 }


 ////////////
 // Window.d

 module window;

 import std.c.windows.windows;

 class Window
 {
 public:
 HANDLE hwnd;

 void MsgBox(LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
 {
 MessageBoxA(null, lpText, lpCaption, uType);
 return;
 }
 }
-- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Mar 20 2005
parent "Alex Stevenson" <ans104 cs.york.ac.uk> writes:
Yes - In D all class objects are references - they behave like  
C++ pointers/references all the time - it takes a little getting used to,  
but once you've done a few it becomes second nature.

I'm pretty sure if I wrote C++ today I'd end up with lots of memory leaks  
because I'd forget to delete all the class objects I new'd.

Alex

On Sun, 20 Mar 2005 18:19:29 +0000 (UTC), NewtoD  
<NewtoD_member pathlink.com> wrote:

 Thank you so much, that did it!  Basically, all instances must be  
 initialized
 with the new keyword, pointer or not?
 :)


 In article <opsnx6jhgd08qma6 mjolnir.spamnet.local>, Alex Stevenson  
 says...
 Hi,

 The problem here is that in App.d you've declared an instance of the
 Window class, but haven't initialised it. You need to write a  
 constructor
 for App which creates a new Window:

 //////////
 // App.d

 module app;

 import window;

 class App
 {
 public:
 Window wnd;
+ this() + { + wnd = new Window(); + }
 }
On Sun, 20 Mar 2005 17:27:29 +0000 (UTC), NewtoD <NewtoD_member pathlink.com> wrote:
 I get no compilation errors but when I debug, I get an access violation
 when the
 call to a class member's method.  I compile this in visual studio .net
 2003 with
 the 3 files (this not an exact replica of the actual code) and the
 custom build
 steps to link them together.  Then I use the vs debugger.  I thought
 this was
 legal. :(

 // main.d
 import app;

 int main(char[][] args)
 {

 app.App myapp = new app.App();
 myapp.wnd.MsgBox("Test","Test", MB_OK);  // This line gets the  
 violation

 return 0;
 }


 //////////
 // App.d

 module app;

 import window;

 class App
 {
 public:
 Window wnd;
 }


 ////////////
 // Window.d

 module window;

 import std.c.windows.windows;

 class Window
 {
 public:
 HANDLE hwnd;

 void MsgBox(LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
 {
 MessageBoxA(null, lpText, lpCaption, uType);
 return;
 }
 }
-- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
-- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Mar 20 2005