Compiler error with static vars/functions

simendsjo simendsjo at gmail.com
Thu Feb 9 11:19:15 PST 2012


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.


More information about the Digitalmars-d-learn mailing list