View previous topic :: View next topic |
Author |
Message |
zane.sims
Joined: 07 Aug 2007 Posts: 40
|
Posted: Sat Jul 12, 2008 3:31 pm Post subject: Class: Access Violation |
|
|
Hello,
I am trying to get into the object oriented features of D. I have the following example program that produces my problem:
Code: | //main.d
import std.stdio;
public class A
{
public int x;
public void set_x(int _x) { x = _x; }
}
void main(char[][] args)
{
A a;
a.set_x(17);
} |
I am using dmd 1.030 on windows XP SP2.
I run a .bat file with:
Code: | dmd main.d
main
pause |
When I run the batch file, I get the output:
Code: | C:\Documents and Settings\Zane\Desktop\zane\Programming\D\test>dmd main.d
C:\Documents and Settings\Zane\Desktop\zane\Programming\D\test>main
Error: Access Violation
C:\Documents and Settings\Zane\Desktop\zane\Programming\D\test>pause
Press any key to continue . . . |
I guess my obvious question is: "Why?"
Thanks for any help that can be provided. _________________ わたしがプログラマだよ! |
|
Back to top |
|
|
MaKo
Joined: 12 Jul 2008 Posts: 5
|
Posted: Sat Jul 12, 2008 4:20 pm Post subject: |
|
|
Add to main:
A a = new A();
Yes, DMD does not warn about this. |
|
Back to top |
|
|
zane.sims
Joined: 07 Aug 2007 Posts: 40
|
Posted: Sat Jul 12, 2008 5:06 pm Post subject: |
|
|
thanks for answering such a simple question. I come from numerous language backgrounds and am still trying to see how D works things out. So if I do an:
A a;
Then that is just an empty, unusable(uninitialized?) class blueprint, correct?
Thanks again for your help. _________________ わたしがプログラマだよ! |
|
Back to top |
|
|
MaKo
Joined: 12 Jul 2008 Posts: 5
|
Posted: Sat Jul 12, 2008 5:16 pm Post subject: |
|
|
zane.sims wrote: | So if I do an:
A a;
Then that is just an empty, unusable(uninitialized?) class blueprint, correct?
|
It is basically a pointer to a class instance. D does not restrict you to initialize class refs, since you may want to initialize in different ways depending on some conditions. Anyway, it is annoying that it does not warn you, when using uninitialized instances. |
|
Back to top |
|
|
|