Sign in to follow this  
crivens

Boost Python - derived class in python's constructor

Recommended Posts

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

Share this post


Link to post
Share on other sites
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&>())
;

Share this post


Link to post
Share on other sites
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::ChTra
itsCRT<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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this