Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
March 20, 2005 Accessing class member methods | ||||
---|---|---|---|---|
| ||||
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; } } |
March 20, 2005 Re: Accessing class member methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to NewtoD | 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/ |
March 20, 2005 Re: Accessing class member methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex Stevenson | 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/ |
March 20, 2005 Re: Accessing class member methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex Stevenson | 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/ |
March 20, 2005 Re: Accessing class member methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to NewtoD | 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/ |
Copyright © 1999-2021 by the D Language Foundation