Sign in to follow this  

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

This topic is 1905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


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

[code]
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();
[/code]

Regards,
Andreas

Share this post


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

Share this post


Link to post
Share on other sites

This topic is 1905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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