Jump to content

[SOLVED] Passing array to member function causes error, but to global function is OK


photo

Recommended Posts

I can pass an array to a global C++ function, from the example given in the topic:

 

    int arrayOfInts[] = ( 0, 1 );
    int i = 0;
    my_array_vector_set( arrayOfInts, i, string(i) );
 

no problem.

However, doing essentially the same call, but to a method of a C++ class gives an error:

 

    ArrayPassingClass passingTest = new ArrayPassingClass();
    passingTest.my_array_vector_set2(  arrayOfInts, i, string(i)  );
 

gives "Interpreter::parse_expression(): arrays can't be used in expressions"

 

In C++:

class ArrayPassingClass
    {
    public:

        // 2.1. Declare a class constructor that will be exported into Unigine script.   
        ArrayPassingClass() { }
        ~ArrayPassingClass() { }

        void my_array_vector_set2(const Variable &id,int index,const Variable &v) {
            ArrayVector vector = ArrayVector::get( Interpreter::get(), id);
            vector.set(index,v);
            }

    };


void my_array_vector_set(const Variable &id,int index,const Variable &v) {
    ArrayVector vector = ArrayVector::get( Interpreter::get(), id);
    vector.set(index,v);
    }
 

 

...

 

    ExternClass<ArrayPassingClass> *my_arrayPassingClass = MakeExternClass<ArrayPassingClass>();
    my_arrayPassingClass->addConstructor();
    my_arrayPassingClass->addFunction( "my_array_vector_set2", &ArrayPassingClass::my_array_vector_set2 );
    Interpreter::addExternClass("ArrayPassingClass", my_arrayPassingClass );

    Interpreter::addExternFunction("my_array_vector_set",MakeExternFunction(&my_array_vector_set,"[]"));
 

Do things need to be done differently when calling a member function?

or, is this a bug?

If so, does it have a work-around?

 

 
Link to comment

Please add array declaration string into the addFunction() call:

my_arrayPassingClass->addFunction( "my_array_vector_set2", &ArrayPassingClass::my_array_vector_set2, "[]," );

Link to comment
  • 2 weeks later...

Thanks, that worked.

 

I could not find any documentation that would have pointed me to that solution.

 

It looks like the C++ reference manual documentaion is lacking for the ExternClass addFunction method for member functions.

 

It appears similar to the Interpreter::addExternFunction / MakeExternFunction type of call combination.

The documentation for that just says "const char * args - Default arguments."

 

I infer from that that the "[]" I needed to add simply provides an empty array if the argument is missing. It's not very clear, to say the least, that it is actually mandatory to supply a default argument when an array is to be passed. I actully do not at all wish a default argument to be supplied if it's accidentally left off, so this appears counterproductive as well as counterintuitive.

 

Are there arguments besides the Unigine containers that require a hint like that?

Link to comment
  • 3 weeks later...
×
×
  • Create New...