FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Class: Access Violation

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
zane.sims



Joined: 07 Aug 2007
Posts: 40

PostPosted: Sat Jul 12, 2008 3:31 pm    Post subject: Class: Access Violation Reply with quote

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
View user's profile Send private message
MaKo



Joined: 12 Jul 2008
Posts: 5

PostPosted: Sat Jul 12, 2008 4:20 pm    Post subject: Reply with quote

Add to main:

A a = new A();

Yes, DMD does not warn about this.
Back to top
View user's profile Send private message
zane.sims



Joined: 07 Aug 2007
Posts: 40

PostPosted: Sat Jul 12, 2008 5:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
MaKo



Joined: 12 Jul 2008
Posts: 5

PostPosted: Sat Jul 12, 2008 5:16 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group