Code fails with linker error. Why?

eles via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 3 21:02:44 PDT 2014


On Friday, 3 October 2014 at 15:47:33 UTC, John Colvin wrote:
> On Friday, 3 October 2014 at 15:44:16 UTC, eles wrote:
>> class ShapeSurface(T) {
>> public:
>> 	int formula();
>
> that means you have a definition of formula elsewhere (which 
> the linker tries to find, but obviously fails. What you want is
>
> class ShapeSurface(T) {
> public:
> 	abstract int formula();

Thanks, but still. The compiler shall not let that code pass down 
to the linker. It has everything it needs to not do that, or it 
shall have. Linker errors shall be simply because the libraries 
are not in place (either not installed, either linking path not 
correctly configured, either broken versions of those libraries).

Then, a quirk of D:

this passes and works correctly:

    surface = cast(T *)this.formula();

this segfaults the produced binary:

   surface = (cast(T *)this).formula();

this fails:

    surface = cast(T)this.formula();

with

    app.d(8): Error: cannot cast this.formula() of type int to 
app.Square

while one has to throw in parentheses to make it work (correctly) 
once again:

    surface = (cast(T)this).formula();

Isn't this funny, that you have to throw in parentheses depending 
on the type of the type you cast to?


More information about the Digitalmars-d-learn mailing list