Linker errors with Interfaces

Igor Lesik curoles at yahoo.com
Tue Aug 10 23:08:49 PDT 2010


I think, it is not linker error at all.
"private" in Transmogrifier prevents overriding; and since those 2 functions are 
not
defined but still in use (vtbl) linker does not find them and complains.
Replace "private" with protected (in CardboardBox also) and it all works.

compiler should say that the functions are not overrided; I am sure a bug report
already filed for this issue

Igor

From: Andrej Mitrovic
Subject: Linker errors with Interfaces

Excerpt from TDPL, p213-214:

interface Transmogrifier
{
    // client interface
    final void thereAndBack()
    {
        transmogrify();
        untransmogrify();
    }
    
    // implementation interface
private:
    void transmogrify();    
    void untransmogrify(); 
}

class CardboardBox : Transmogrifier
{
    override private void transmogrify()
    {
        // get in the box
    }
    
    override private void untransmogrify()
    {
        // get out of the box
    }
}

void play()
{
    writeln("just playing");
}

void aDayInLife(Transmogrifier device, string mood)
{
    if (mood == "play")
    {
        device.transmogrify();
        play();
        writeln(typeid(device));
        device.untransmogrify();
    }
    else if (mood == "experiment")
    {
        device.thereAndBack();
    }
}

import std.stdio;

void main()
{
    aDayInLife(new CardboardBox, "play");
}


Calling device.transmogrify() and device.untransmogrify() should not even be 
allowed to compile. I can comment these out, but the linker will still fail when 

there's a call to device.thereAndBack(); device is an object that inherits from 
interface Transmogrifier, and "thereAndBack()" is a public method of the 
interface, so I'm not sure whats wrong with that call.

I get these in both cases:

output\interface_test.obj(interface_test) 
Error 42: Symbol Undefined _D14interface_test14Transmogrifier12transmogrifyMFZv
output\interface_test.obj(interface_test) 
Error 42: Symbol Undefined 
_D14interface_test14Transmogrifier14untransmogrifyMFZv


One more thing, here's a class that inherits from the same interface, but tries 
to make the overriden methods non-private by mistake:
interface Transmogrifier
{

    // client interface
    final void thereAndBack()
    {
        transmogrify();
        untransmogrify();
    }
    
    // implementation interface
private:
    void transmogrify();      
    void untransmogrify();  
}

class CardboardBox : Transmogrifier
{
    override void transmogrify()
    {
        // get in the box
    }
    
    override void untransmogrify()
    {
        // get out of the box
    }
}

I get these errors:
interface_test.d(23): Error: function interface_test.CardboardBox.transmogrify 
does not override any function
interface_test.d(28): Error: function interface_test.CardboardBox.untransmogrify 

does not override any function

The book has nice plausible error message alternatives:
// Error: Cannot change protection of transmogrify from private to public
// Error: Cannot change protection of untransmogrify from private to public

I'd prefer if DMD put out those kinds of error messages in this case (or 
something similar).



      


More information about the Digitalmars-d mailing list