Calling a static method ?
Lars Ivar Igesund
larsivar at igesund.net
Thu Jul 13 08:05:39 PDT 2006
andy.dwelly at safedataco.com wrote:
> I'm taking a first look at D. I have a singleton class:
>
> class EventQueue {
> private static EventQueue _instance;
>
> private this() {
> }
>
> public static EventQueue instance() {
> if (_instance is null) {
> _instance = new EventQueue;
> }
>
> return _instance;
> }
> }
>
> int main(char[][] args) {
> auto var = EventQueue.instance();
> return 0;
> }
>
> which compiles quite happiliy (WinXP, dmd 0.162) with dmd -c EventQueue.d
> , I have another file Frame.d:
>
> import Component;
> import EventQueue;
>
> class Frame: Component {
> private EventQueue _queue;
>
> private this(char[] title) {
> EventQueue q = EventQueue.instance;
> }
>
>
> public void add(Component c) {
> }
> }
>
> ..which is trying to create an event queue. When I compile this with dmd
> -c Frame.d I get an:
>
> Frame.d(19): undefined identifier module EventQueue.instance
>
> What am I doing wrong here ? The fact that the main function in the
> EventQueue.d file suggests that its some kind of linker or import error,
> the instance() method in EventQueue seems to be OK.
>
> Can somebody give me some guidance please.
>
>
> Andy Dwelly
Hmm, the problem here is that the module is named EventQueue too. You can
try to add () to your instance invocation in Frame and see if that helps,
otherwise you can change the module name (and filename) to lowercase. If
you want this to be in a larger project, you might want to add an extra
level to your hierarchy, such that the module name becomes
myproject.EventQueue.
--
Lars Ivar Igesund
blog at http://larsivi.net
DSource & #D: larsivi
More information about the Digitalmars-d-learn
mailing list