Code fails with linker error. Why?

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


This is under Linux 64 with both dmd 2.066 (and latest gdc-4.9):

=================================================================
class ShapeSurface(T) {
public:
	int formula();
	int getSurfaceBy100() {
		int surface;
		surface = cast(T *)this.formula();
		return surface*100;
	};
};

class Square: ShapeSurface!Square
{
public:
	int squareSize = 8;
	override int formula() {
		return squareSize*squareSize;
	}
};

int main() {

	Square square = new Square();
	square.getSurfaceBy100();

	return 0;
}
=================================================================

It fails with linker error:

dmd app.d
app.o:(.data._D3app31__T12ShapeSurfaceTC3app6SquareZ12ShapeSurface6__vtblZ+0x28): 
undefined reference to 
`_D3app31__T12ShapeSurfaceTC3app6SquareZ12ShapeSurface7formulaMFZi'
collect2: error: ld returned 1 exit status
--- errorlevel 1

OK, maybe the cast is broken (well, to a pointer...), so changed 
this:

		surface = cast(T *)this.formula();
into this:
		T cls = cast(T)this;
		surface = cls.formula();

giving:

dmd app.d
app.o:(.data._D3app31__T12ShapeSurfaceTC3app6SquareZ12ShapeSurface6__vtblZ+0x28): 
undefined reference to 
`_D3app31__T12ShapeSurfaceTC3app6SquareZ12ShapeSurface7formulaMFZi'
collect2: error: ld returned 1 exit status
--- errorlevel 1

So, if you could tell me:

1) Why the compiler defer to the linker
2) What is the CRTP equivalent here

Many thanks.


More information about the Digitalmars-d-learn mailing list