Joel
Joel

Reputation: 2361

Error binding properties and functions in emscripten

I'm trying to use emscripten to compile a c++ class and expose bindings. I'm running into an error from the compiler.

#include <emscripten/bind.h>
#include <emscripten/emscripten.h>


using namespace emscripten;

class  MyClass {
private:
    int _year;
    int _month;
    int _day;
public:
    MyClass() { }
    MyClass(int year, int month, int day);

    int getMonth();
    void setMonth(int);
    int getYear();
    void setYear(int);
    int getDay();
    void setDay(int);
    bool isLeapYear();
    int daysInMonth();

    void increment();
    void decrement();
};

EMSCRIPTEN_BINDINGS(my_sample_class) {
class_<MyClass>("MyClass") 
    .constructor<>()
    .constructor<int, int, int>()
    .function("getMonth",  &MyClass::getMonth)
    .function("increment", &MyClass::increment)
    .function("decrement", &MyClass::decrement)
    .property("year",&MyClass::getYear, &MyClass::setYear )
    //.property("month", &MyClass::getMonth, &MyClass::setMonth )
    //.property("day",&MyClass::getDay, &MyClass::setDay )
    ;
}

The compiler has no problems with the constructors or the function binding. I run into a problem with the property binding. I only have one uncommented to minimize the errors that I get back (they are just repeats of the same error but for different lines). Here are the errors that I'm getting back.

In file included from MyDate.cpp:1:
In file included from ./MyDate.h:2:

emscripten/bind.h:1393:26: error: implicit instantiation of undefined template 'emscripten::internal::GetterPolicy<int (MyClass::*)()>'

        auto gter = &GP::template get<ClassType>;
                     ^
./MyDate.h:37:6: note: in instantiation of function template specialization 'emscripten::class_<MyClass, emscripten::internal::NoBaseClass>::property<int (MyClass::*)(), void (MyClass::*)(int)>' requested here
.property("year",&MyClass::getYear, &MyClass::setYear )
 ^ 



 emscripten/bind.h:569:16: note: template is declared here

    struct GetterPolicy;
           ^
emscripten/bind.h:1399:33: error: implicit instantiation of undefined template 'emscripten::internal::GetterPolicy<int (MyClass::*)()>'
            TypeID<typename GP::ReturnType>::get(),
                            ^
emscripten\1.38.21\system\include\emscripten/bind.h:569:16: note: template is declared here
    struct GetterPolicy;
           ^
2 errors generated.
shared:ERROR: compiler frontend failed to generate LLVM bitcode, halting

I've looked up binding examples and it appears I'm using the right syntax. Does any one have any idea of what I might be doing wrong?

Upvotes: 6

Views: 1336

Answers (1)

Joel
Joel

Reputation: 2361

Found the problem!

The getter functions must be marked as const to avoid these errors. EX: int getMonth() const;

Upvotes: 6

Related Questions