Advertisement

Can't create a template class with a default constructor.

Started by October 27, 2012 02:41 PM
3 comments, last by WitchLord 12 years, 1 month ago
I'm created a template class,
[source lang="cpp"]
r = engine->RegisterObjectType("myObj", 0, asOBJ_REF | asOBJ_NOCOUNT); X_ASSERT0(r >= 0);
r = engine->RegisterObjectType("x<class T>", 0, asOBJ_REF | asOBJ_TEMPLATE); assert( r >= 0 );

r = engine->RegisterObjectBehaviour("x<T>", asBEHAVE_FACTORY, "x<T>@ f(int&in)",
asFUNCTIONPR(xFactory, (asIObjectType*), x*), asCALL_CDECL); assert( r >= 0 );


r = engine->RegisterObjectBehaviour("x<T>", asBEHAVE_ADDREF, "void f()",
asMETHOD(x, addRef), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("x<T>", asBEHAVE_RELEASE, "void f()",
asMETHOD(x, release), asCALL_THISCALL); assert( r >= 0 );
[/source]
then compile my test as code.:
[source lang="cpp"]
void test()
{
x<myObj> xxx;
}

[/source]
running it will got an error:

ERR : No default constructor for object of type 'x'.

currently, my solution is use template specializations, register a factory for all my object types.
Hi, thanks for the report.

I haven't been able to reproduce this problem with the latest WIP version, though. Which version of AngelScript are you using?

This is the test that I wrote:


asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
engine->SetMessageCallback(asMETHOD(CBufferedOutStream, Callback), &bout, asCALL_THISCALL);

r = engine->RegisterObjectType("myObj", 0, asOBJ_REF | asOBJ_NOCOUNT); assert(r >= 0);
r = engine->RegisterObjectType("x<class T>", 0, asOBJ_REF | asOBJ_TEMPLATE); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("x<T>", asBEHAVE_FACTORY, "x<T>@ f(int&in)", asFUNCTION(0), asCALL_CDECL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("x<T>", asBEHAVE_ADDREF, "void f()", asFUNCTION(0), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("x<T>", asBEHAVE_RELEASE, "void f()", asFUNCTION(0), asCALL_THISCALL); assert( r >= 0 );
asIScriptModule *mod = engine->GetModule("test", asGM_ALWAYS_CREATE);
mod->AddScriptSection("test",
"void test() \n"
"{ \n"
" x<myObj> xxx; \n"
"} \n");
bout.buffer = "";
r = mod->Build();
if( r < 0 )
TEST_FAILED;
if( bout.buffer != "" )
{
printf("%s", bout.buffer.c_str());
TEST_FAILED;
}
engine->Release();


Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
Seems my test code has lost one factory registration,

[source lang="cpp"]

r = engine->RegisterObjectType("myObj", 0, asOBJ_REF | asOBJ_NOCOUNT); assert(r >= 0);
r = engine->RegisterObjectType("x<class T>", 0, asOBJ_REF | asOBJ_TEMPLATE); assert( r >= 0 );

r = engine->RegisterObjectBehaviour("x<T>", asBEHAVE_FACTORY, "x<T>@ f(int&in, int)", asFUNCTION(xFactory2), asCALL_CDECL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("x<T>", asBEHAVE_FACTORY, "x<T>@ f(int&in)", asFUNCTION(xFactory), asCALL_CDECL); assert( r >= 0 );


r = engine->RegisterObjectBehaviour("x<T>", asBEHAVE_ADDREF, "void f()", asMETHOD(x, addRef), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("x<T>", asBEHAVE_RELEASE, "void f()", asMETHOD(x, release), asCALL_THISCALL); assert( r >= 0 );

[/source]
I'm using version 2.24.1, and I found that when I register default constructor first, the problem solved.
I tested it on the latest WIP version, the same problem.
No problem. I'll try again with the new information.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I've fixed the problem in revision 1451.

Thanks,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement