我似乎在重载我的某个类的opIndexAssign时遇到了一些问题。
我有一个类;JSObject,它的定义如下:
alias char[] String;..。
class JSObject : Dobject
{
/*****************************************************************
* Constructors
******************************************************************/
this( Dobject dobj )
{
super( dobj ) ;
}
this()
{
super( null ) ;
}
this( AssociativeArray data )
{
// initiate
super( null ) ;
// then populate
foreach( k, v ; data )
{
this[ k ] = v ;
}
}
public void opIndexAssign( String key , String val )
{
Value* v = new Value() ;
v.putVstring( val ) ;
this.Put(key, v , DontDelete);
}
public void opIndexAssign( String key , Dobject dobj )
{
Value* v = new Value() ;
v.putVobject( dobj ) ;
this.Put(key, v , DontDelete);
}
public void opIndexAssign( String key , JSObject jso )
{
Value* v = new Value() ;
v.putVobject( jso ) ;
this.Put(key, v , DontDelete);
}
public Value* opIndex( String key )
{
return this.Get( key );
}
}Dobject超类重载了put()和get()方法,我正在尝试包装它们,以便可以将它们作为关联数组进行访问:
77: JSObject jso = new JSObject() ;
78: jso[ "foo" ] = "bar" ;
79:
80: JSObject jsoParent = new JSObject() ;
81: jsoParent[ "child" ] = jso ;它适用于String,String方法,但是当我尝试使用JSObject作为值时,它失败了。
test2.d => test2
+ c:\dmd\dsss\bin\rebuild.exe -version=PhobosCompatibility -w -Idsss_imports\ -I. -S.\ -Ic:\dmd\dsss\include\d -Sc:\dmd\dsss\lib\ -Ic:\dmd\dsss\include\d -Sc:\dmd\dsss\lib -oqdsss_objs\D -debug -gc test2.d -oftest2
test2.d(81): Error: function dmdscripttest.JSObject.opIndexAssign (char[],char[]) does not match parameter types (JSObject,char[5u])
test2.d(81): Error: cannot implicitly convert expression (jso) of type dmdscripttest.JSObject to char[]
test2.d(81): Error: cannot implicitly convert expression ("child") of type char[5u] to dmdscripttest.JSObject
Error: Command failed, aborting.
Command c:\dmd\dsss\bin\rebuild.exe returned with code 1, aborting.我对我做错了什么感到有点迷茫。这就像编译器试图将其转换为适合opIndexAssign( String,String )而不是opIndexAssign( String,JSObject )方法。
我是否错误地定义了opIndexAssign函数?
提前谢谢你,
发布于 2011-07-09 20:51:17
问题是opIndexAssigne首先需要值,然后才需要键(或索引)
http://www.d-programming-language.org/operatoroverloading.html#Assignment
因此,您需要将其定义为
public void opIndexAssign( String val , String key)
{
Value* v = new Value() ;
v.putVstring( val ) ;
this.Put(key, v , DontDelete);
}
public void opIndexAssign( Dobject dobj , String key)
{
Value* v = new Value() ;
v.putVobject( dobj ) ;
this.Put(key, v , DontDelete);
}
public void opIndexAssign( JSObject jso , String key)
{
Value* v = new Value() ;
v.putVobject( jso ) ;
this.Put(key, v , DontDelete);
}这样做的原因是您可以为索引定义一个vararg
https://stackoverflow.com/questions/6634457
复制相似问题