Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
July 31, 2007 new class howto? | ||||
---|---|---|---|---|
| ||||
hi, why is it that one can not create a class with the following: module A extern (Windows): uint P_Start(char * pParam); void P_Stop(); module A import A; class XXX { public: this() {} uint start(char *yy) { return P_Start(yy); } void start(char *yy) { P_Stop(); } } module C XXX tester = new XXX(); it will always generate an error during compilation: non-constant expression when i try to do that in a function, then i will get an exception. |
July 31, 2007 Re: new class howto? | ||||
---|---|---|---|---|
| ||||
Posted in reply to newbie | sorry, it should be module A extern (Windows): uint P_Start(char * pParam); void P_Stop(); module B import A; class XXX { public: this() {} uint start(char *yy) { return P_Start(yy); } void start(char *yy) { P_Stop(); } } module C import B; XXX tester = new XXX(); |
July 31, 2007 Re: new class howto? | ||||
---|---|---|---|---|
| ||||
Posted in reply to newbie | newbie wrote:
> module C
>
> XXX tester = new XXX();
>
>
> it will always generate an error during compilation:
>
> non-constant expression
>
> when i try to do that in a function, then i will get an exception.
You can only use constant expressions as initializers for global variables, so dynamic memory allocation is disallowed. To do what you want, use:
---
module C;
XXX tester;
static this() {
tester = new XXX();
}
---
|
July 31, 2007 Re: new class howto? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | Frits van Bommel Wrote:
> newbie wrote:
> > module C
> >
> > XXX tester = new XXX();
> >
> >
> > it will always generate an error during compilation:
> >
> > non-constant expression
> >
> > when i try to do that in a function, then i will get an exception.
>
> You can only use constant expressions as initializers for global variables, so dynamic memory allocation is disallowed. To do what you want, use:
> ---
> module C;
>
> XXX tester;
>
> static this() {
> tester = new XXX();
> }
> ---
thank you for the reply.
it will compile, but it will fail with an exception.
that will also happen if i declare the variable in a function and than try to do my new.
void testers() {
XXX tester = new XXX(); <---- exception
tester.Stop();
}
could it be, that the extern(Windows) is a problem?
|
July 31, 2007 Re: new class howto? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | Frits van Bommel Wrote: > newbie wrote: > > module C > > > > XXX tester = new XXX(); > > > > > > it will always generate an error during compilation: > > > > non-constant expression > > > > when i try to do that in a function, then i will get an exception. > > You can only use constant expressions as initializers for global variables, so dynamic memory allocation is disallowed. To do what you want, use: > --- > module C; > > XXX tester; > > static this() { > tester = new XXX(); > } > --- it is seeminglx not possible to call d functions or initialize d classes from a function that is decorated with extern(Windows) extern (Windows) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { testery(); DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_MAIN), null, &Dialog_Main); return 0; } void testery() { XXX testerx = new XXX; testerx.Stop(); } how does one call a D function or do a XXX testerx = new XXX; in such a function? |
July 31, 2007 Re: new class howto? | ||||
---|---|---|---|---|
| ||||
Posted in reply to newbie | newbie wrote: > Frits van Bommel Wrote: > >> newbie wrote: >>> module C >>> >>> XXX tester = new XXX(); >>> >>> >>> it will always generate an error during compilation: >>> >>> non-constant expression >>> >>> when i try to do that in a function, then i will get an exception. >> You can only use constant expressions as initializers for global variables, so dynamic memory allocation is disallowed. To do what you want, use: >> --- >> module C; >> >> XXX tester; >> >> static this() { >> tester = new XXX(); >> } >> --- > it is seeminglx not possible to call d functions or initialize d classes from a function that is decorated with extern(Windows) > > extern (Windows) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { > testery(); > DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_MAIN), null, &Dialog_Main); > return 0; > } > > > void testery() { > XXX testerx = new XXX; > testerx.Stop(); > } > > > how does one call a D function or do a XXX testerx = new XXX; in such a function? > > Wait; you're calling this from WinMain? The code you just supplied will not work because you haven't initialised the garbage collector, nor run module ctors, nor run unittests. See http://digitalmars.com/d/windows.html for an example of what your WinMain should look like. -- Daniel |
July 31, 2007 Re: new class howto? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep | Daniel Keep Wrote:
>
>
> newbie wrote:
> > Frits van Bommel Wrote:
> >
> >> newbie wrote:
> >>> module C
> >>>
> >>> XXX tester = new XXX();
> >>>
> >>>
> >>> it will always generate an error during compilation:
> >>>
> >>> non-constant expression
> >>>
> >>> when i try to do that in a function, then i will get an exception.
> >> You can only use constant expressions as initializers for global variables, so dynamic memory allocation is disallowed. To do what you want, use:
> >> ---
> >> module C;
> >>
> >> XXX tester;
> >>
> >> static this() {
> >> tester = new XXX();
> >> }
> >> ---
> > it is seeminglx not possible to call d functions or initialize d classes from a function that is decorated with extern(Windows)
> >
> > extern (Windows) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
> > testery();
> > DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_MAIN), null, &Dialog_Main);
> > return 0;
> > }
> >
> >
> > void testery() {
> > XXX testerx = new XXX;
> > testerx.Stop();
> > }
> >
> >
> > how does one call a D function or do a XXX testerx = new XXX; in such a function?
> >
> >
>
> Wait; you're calling this from WinMain?
>
> The code you just supplied will not work because you haven't initialised the garbage collector, nor run module ctors, nor run unittests. See http://digitalmars.com/d/windows.html for an example of what your WinMain should look like.
>
> -- Daniel
that was the wright thing to do - thank you all so much.
|
August 22, 2007 Re: new class howto? | ||||
---|---|---|---|---|
| ||||
Posted in reply to newbie | "newbie" <newbie@nospam.com> wrote in message news:f8n64d$1bev$1@digitalmars.com... > hi, > > why is it that one can not create a class with the following: > > module A > extern (Windows): You're missing the semicolon after the module name. <snip> > it will always generate an error during compilation: > > non-constant expression On what compiler version are you getting that? Please post compiler messages in full. Stewart. |
Copyright © 1999-2021 by the D Language Foundation