digitalmars.D.learn - Compiler error with static vars/functions
- Oliver Plow (43/43) Feb 09 2012 Hello,
- simendsjo (3/43) Feb 09 2012 As your class is named the same as your module, writeln(Foo.z) looks for...
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
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, OliverAs 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