C++ compile error: DynamicTextFont

Hi,

I try to compile this simple function (see http://www.panda3d.org/manual/index.php/Text_Fonts):

#include "fontPool.h"
#include "staticTextFont.h"
#include "dynamicTextFont.h"

void f() {
	PT(TextFont) font=FontPool::load_font("arial.ttf"); // OK
	PT(StaticTextFont) sfont=FontPool::load_font("arial.egg");
	PT(DynamicTextFont) dFont = FontPool::load_font("arial.ttf");
}

with this command line:

$ g++ -c dyn_text_font.cxx -o dyn_text_font.o -O2 -pthread -msse2 -I/usr/include/python2.7   -I/usr/include/freetype2 -I/usr/include/panda3d

But I got theses errors:

dyn_text_font.cxx: In function 'void f()':
dyn_text_font.cxx:7:58: error: invalid conversion from 'TextFont*' to 'PointerTo<StaticTextFont>::To* {aka StaticTextFont*}' [-fpermissive]
/usr/include/panda3d/pointerTo.I:22:8: error:   initializing argument 1 of 'PointerTo<T>::PointerTo(PointerTo<T>::To*) [with T = StaticTextFont, PointerTo<T>::To = StaticTextFont]' [-fpermissive]
dyn_text_font.cxx:8:61: error: invalid conversion from 'TextFont*' to 'PointerTo<DynamicTextFont>::To* {aka DynamicTextFont*}' [-fpermissive]
/usr/include/panda3d/pointerTo.I:22:8: error:   initializing argument 1 of 'PointerTo<T>::PointerTo(PointerTo<T>::To*) [with T = DynamicTextFont, PointerTo<T>::To = DynamicTextFont]' [-fpermissive]

Was I’m doing something wrong somewhere, or there is a bug (eventually in the manual)?

OS: Ubuntu 12.04 64bits
GCC: 4.6.3
Panda3D: 1.8.0

Thank you.

Since TextFont* FontPool::load_font and TextFont is a base class for both StaticTextFont and DynamicTextFont, you cannot assign the returned pointer to any of the derived class. So yes, the manual is in error in the case of DynamicTextFont. The code should go like this:

PT(DynamicTextFont) dFont = new DynamicTextFont("arial.ttf");

Concerning StaticTextFont, it will indirectly load a font from an egg file. You can take a look into FontPool::ns_load_font to get an idea of how to do it.

To conclude, it looks like the current implementation of FontPool::load_font loads either a StaticTextFont or a DynamicTextFont but returns it in the form of its base class TexFont. Perhaps you could static_cast it if you are certain of the content of the file you are loading from.

Thank you for your reply.

Yes, I have use a static_cast, but since I’m not a C++ expert, and learning Panda3D, I wasn’t very sure.

Instead of a static_cast, for Panda classes you should use this:

PT(DynamicTextFont) a = DCAST(DynamicTextFont, b);

Include “dcast.h” to make the macro available, if necessary.