Compile-type array of classes?

Steven Schveighoffer schveiguy at yahoo.com
Sun Feb 23 19:18:19 PST 2014


On Sun, 23 Feb 2014 17:14:16 -0500, Gordon <me at home.com> wrote:

> Compiling with the marked writeln, produces an "underfined reference"  
> error:
> ====
> $ rdmd animals.d
> /tmp/.rdmd-34574/rdmd-animals.d-796CFD5A46BFE9DF13BF873F65EA656C/objs/animals.o:  
> In function `_Dmain':
> animals.d:(.text._Dmain+0xdf): undefined reference to  
> `_D7animals6Animal4nameFNdZAya'
> collect2: error: ld returned 1 exit status
> --- errorlevel 1
> ====
>
> BTW - a work-around is simple, my question is about the theory behind  
> this.
>
> I understand that "Animal::name" is undefined (since "Animal" is an  
> interface).

Note that Animal.name (Animal::name is C++, we use dots in D) does not  
define any requirements for derived mechanisms. And calling it on a base  
class or interface instance does not call the derived class' version.

You should not make it static, it should be virtual. Otherwise, it will  
not be a virtual call. The reason you get the error is because it's trying  
to call Animal.name, and you haven't defined it.

> It is likely because of the "static", but if I remove the "static" than  
> the 'foreach' loop won't compile, because "name" is not static.

You don't need the whole static name business, D is better than that :)

string input_from_user = "Dog"; // must be exact for this example, but you  
could use case insensitive compare if you wanted

...

if(i.stringof == input_from_user)

...

-Steve


More information about the Digitalmars-d-learn mailing list