Emscripten中的错误绑定属性和功能

时间:2019-01-03 00:43:55

标签: c++ llvm emscripten webassembly

我正在尝试使用emscripten编译c ++类并公开绑定。我从编译器遇到错误。

#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 )
    ;
}

编译器在构造函数或函数绑定方面没有问题。我遇到了属性绑定问题。我只有一个未注释的内容可以最大程度地减少我返回的错误(它们只是相同错误的重复,但适用于不同的行)。这是我回来的错误。

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

我查看了绑定示例,看来我使用的是正确的语法。有谁知道我可能做错了什么?

1 个答案:

答案 0 :(得分:0)

发现问题了!

getter函数必须标记为const以避免这些错误。 例如:     int getMonth()const;