www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compiler error with static vars/functions

reply "Oliver Plow" <saxo123 gmx.de> writes:
Hello,

I'm fighting with a strange compiler error. This here compiles and runs fine:

---------- main.d -------------

class Foo
{
    static int z = 4;
    static int bar() { return 6; }
    int foobar() { return 7; }
}

int main(string[] argv)
{
    writeln(Foo.z);
    writeln(Foo.bar()); // produces 6
    Foo f;
    writeln(f.bar()); // produces 6;
    writeln(f.foobar());
    return 0;
}

Whereas this does not compile:


---------- main.d -------------

import Foo;

int main(string[] argv)
{
    writeln(Foo.z);	// Error: undefined identifier module Foo.z
    writeln(Foo.bar()); // Error: undefined identifier module Foo.bar
    Foo f;
    writeln(f.bar());
    writeln(f.foobar());
    return 0;
}

---------- Foo.d ----------

class Foo
{
	public static int z = 4;
	public static int bar() { return 6; }
	public int foobar() { return 7; }
}

This is a bit strange for me. Apparently, must be some kind of import problem
importing Foo. But I don't see how ...

Thanks for any hints.
Cheers, Oliver


-- 
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de
Feb 09 2012
parent simendsjo <simendsjo gmail.com> writes:
On 02/09/2012 02:57 PM, Oliver Plow wrote:
 Hello,

 I'm fighting with a strange compiler error. This here compiles and runs fine:

 ---------- main.d -------------

 class Foo
 {
      static int z = 4;
      static int bar() { return 6; }
      int foobar() { return 7; }
 }

 int main(string[] argv)
 {
      writeln(Foo.z);
      writeln(Foo.bar()); // produces 6
      Foo f;
      writeln(f.bar()); // produces 6;
      writeln(f.foobar());
      return 0;
 }

 Whereas this does not compile:


 ---------- main.d -------------

 import Foo;

 int main(string[] argv)
 {
      writeln(Foo.z);	// Error: undefined identifier module Foo.z
      writeln(Foo.bar()); // Error: undefined identifier module Foo.bar
      Foo f;
      writeln(f.bar());
      writeln(f.foobar());
      return 0;
 }

 ---------- Foo.d ----------

 class Foo
 {
 	public static int z = 4;
 	public static int bar() { return 6; }
 	public int foobar() { return 7; }
 }

 This is a bit strange for me. Apparently, must be some kind of import problem
importing Foo. But I don't see how ...

 Thanks for any hints.
 Cheers, Oliver
As your class is named the same as your module, writeln(Foo.z) looks for z in module Foo. Foo.Foo.z shoulde give you module.class.instance.
Feb 09 2012