Error: 'this' is only defined in non-static member functions

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Nov 22 23:46:18 UTC 2017


On Wednesday, November 22, 2017 22:45:53 A Guy With a Question via 
Digitalmars-d-learn wrote:
> On Wednesday, 22 November 2017 at 22:37:46 UTC, Steven
>
> Schveighoffer wrote:
> > On 11/22/17 5:36 PM, Steven Schveighoffer wrote:
> >> This allows access to the outer class's members. So you need
> >> an instance to instantiate.
> >>
> >> I bet it's the same for interfaces.
> >
> > All that being said, the error message is quite lousy.
> >
> > -Steve
>
> Yup that worked. Thanks!
>
> Out of curiosity, what does static mean in that context? When I
> think of a static class I think of them in the context of Java or
> C# where they can't be instantiated and where they are more like
> namespaces that you can't directly import the contents of.

A static, nested class does not have access to the class that it's in. It's
basically a normal class that's namespaced within another class, whereas
non-static, nested class is associated with a specific instance of the class
and has access to that class instance via its outer member.

https://dlang.org/spec/class.html#nested
http://ddili.org/ders/d.en/nested.html

Similarly, if a struct, class, or function is nested within a function, and
it is not marked with static, then it has access to the function that it's
in, whereas if it's marked with static, then it acts more like a normal
struct/class/function and does not have access to the function that it's in
and is just namespaced within that function.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list