Skip to main content
GameDev.net gamedev.net
🔒 Locked

Boost Python - derived class in python's constructor

Started by crivens Apr 23, 2008 at 7:19 AM 9 replies 4.6k views
Original Post
crivens
crivens
Another basic question to help my understanding of Boost::Python. Say I have a class in C++ CEntity that is exported to Python using:
void export_entity()
{
	class_ <CObject,boost::noncopyable>("Object",no_init)
		;

	class_<CEntity,boost::noncopyable,bases<CObject>>("Entity", init<const CString&>())
		;
}
If I derive from CEntity in Python:
class MyEntity(Entity):
	def __init__(self, name):
		Entity.__init__(self, name)
		print "MyEntity - " + name
I get a traceback when calling the super class' __init__ function.
Boost.Python.ArgumentError: Python argument types in
    Entity.__init__(MyEntity, str)
did not match C++ signature:
    __init__(struct _object *, class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > >)
Do I need to wrap (wrapper.h?) the constructor in Boost? I presumed this would "just work" as it's fairly standard/basic OO/Python stuff. Unfortunately I'm trying to add scripting to an MFC app of mine, so joyous days are ahead! Thanks
SiCrane
SiCrane
It looks like you didn't register CString with Python.
crivens
crivens
I did, I just forgot to paste it in here:

	class_ <CString>("CString")		;	class_ <CObject,boost::noncopyable>("Object",no_init)		;	class_<CEntity,boost::noncopyable,bases<CObject>>("Entity", init<const CString&>())		;
SiCrane
SiCrane
Given your class registration why would Python know that passing a string would have anything to do with the CString class?
crivens
crivens
Yeah I just realised that when I finished responding to your last post. So I'm reading up on converters. Guess I made a huge presumption!
crivens
crivens
For anyone reading, I followed the example at the bottom of this page, replace custom_string with CString.

http://www.boost.org/doc/libs/1_35_0/libs/python/doc/v2/faq.html
crivens
crivens
That works nicely except I have memory leaks when I exit the app in debug mode (VC2005). VC2005 gives no indication where they are. Ugh....
crivens
crivens
It works until I try to access an exported data member of type CString:

TypeError: No Python class registered for C++ class class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > >


If I add this to the BOOST::Python exports:

	class_ <CString>("CString")		;


I get an assertion line 160 in registry.cpp, Expression: slot==0

Any ideas? Thanks
SiCrane
SiCrane
Insufficient information. What does your exported data member look like? What does the class that contains that data member look like? What version of boost::python are you using? What does the code that accesses that data member look like?
crivens
crivens
Boost 1.34.1, Win32 installer from the Boost Consulting site.

Data member:

public:	CString	mType;		// class name


I can't post the class - proprietary code etc.... But it's basically:

class CEntity :public CObject...


I don't have any code that accesses this member yet - I'm just trying to export it first. The assert is from Boost trying to register the CString class twice I think.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.