I have two cpp files called in the Main.cpp file. This code is to be called from the ams.js file. I'm using the Embind Compiler to call WASM from JS.
Here is my sample code:
class.h:
class CLASS{
public:
int VARIABLE;
void FUNCTION();
};
class.cpp:
#include "CLASS.h"
void CLASS::FUNCTION()
{
VARIABLE = 5;
std::cout << "out : "+VARIABLE << std::endl;
}
Main.cpp:
#include <emscripten/bind.h>
#include "CLASS.h"
using namespace emscripten;
class MyClass
{
public:
MyClass(int x)
: x(x)
{}
int getCharCount(std::string strKey)
{
CLASS a;
a.FUNCTION();
return 0;
}
private:
int x;
};
EMSCRIPTEN_BINDINGS(my_class_example) {
class_<MyClass>("MyClass")
.constructor<int>()
.function("getCharCount", &MyClass::getCharCount);
}
For compiling:
emcc --bind Main.cpp -o main.js
Calling the function in Render.js:
var instance = new Module.MyClass();
if (instance){
var mainee = instance.getCharCount("hi")
console.log("Somrthing is There");
}else{
console.log("Somrthing Wrong");
}
instance.delete();
Output error:
main3.js:2780 Uncaught BindingError: Tried to invoke ctor of MyClass with invalid number of parameters (0) - expected (1) parameters instead!
How can I solve this issue?
Use separate compilation.
emcc --bind -c class.cpp
emcc --bind -c main.cpp
emcc --bind class.o main.o -o main.js
But BindingError is caused by new Module.MyClass();
, try new Module.MyClass(123);
.