绑定c++;第一类';我找不到构造函数 我正在研究将一个简单C++类绑定到JavaScript中。我是通过Emscripten 2.0.17实现的
我绑定了hello_world.cpp绑定c++;第一类';我找不到构造函数 我正在研究将一个简单C++类绑定到JavaScript中。我是通过Emscripten 2.0.17实现的,javascript,c++,bind,emscripten,Javascript,C++,Bind,Emscripten,我绑定了hello_world.cpp #include <iostream> class hello { public: hello() { std::cout << "Hello world!!!\n"; } }; #include <emscripten/bind.h> namespace emcc = emscripten; EMSCRIPTEN_BINDINGS(jshello) {
#include <iostream>
class hello {
public:
hello() { std::cout << "Hello world!!!\n"; }
};
#include <emscripten/bind.h>
namespace emcc = emscripten;
EMSCRIPTEN_BINDINGS(jshello) {
emcc::class_<hello>("hello")
.constructor<>();
}
我尝试使用node main.js运行node 15.14.0
我犯了以下错误
/home/kubuntu/Desktop/c++/hello_world/hello_world.js:117
throw ex;
^
TypeError: hw.hello is not a constructor
at Object.<anonymous> (/home/kubuntu/Desktop/c++/hello_world/main.js:3:9)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47
/home/kubuntu/Desktop/c++/hello\u world/hello\u world.js:117
掷骰子;
^
TypeError:hw.hello不是构造函数
反对。(/home/kubuntu/Desktop/c++/hello_world/main.js:3:9)
at模块编译(节点:内部/modules/cjs/loader:1092:14)
在Object.Module.\u extensions..js(节点:internal/modules/cjs/loader:1121:10)
at Module.load(节点:内部/模块/cjs/加载器:972:32)
at功能模块加载(节点:内部/模块/cjs/加载程序:813:14)
在Function.executeUserEntryPoint[作为runMain](节点:内部/modules/run_main:76:12)
节点处:内部/主/运行\u主\u模块:17:47
有人能告诉我发生了什么事吗?提前感谢您只需像这样调用
hello
构造函数:
var x = new hello();
一旦您需要js模块,它的内容将不会嵌套到您分配给它的变量中,而是成为当前作用域的顶层。您只需像这样调用
hello
构造函数:
var x = new hello();
一旦您需要js模块,它的内容将不会嵌套到您分配给它的变量中,而是成为当前作用域的顶级。使用Emscripten公开类
如果你想用EnScript来公开C++类,你需要这些步骤,编写C++代码:
// hello_world.cpp
#include <iostream>
class Hello {
public:
Hello() {
std::cout << "Hello world!!!\n" << std::endl;
}
void saySomething() {
std::cout << "something" << std::endl;
}
};
#include <emscripten/bind.h>
using namespace emscripten;
EMSCRIPTEN_BINDINGS(jshello) {
class_<Hello>("Hello")
.constructor<>()
.function("saySomething", &Hello::saySomething);
}
我们将代码包装到OnRuntimeInitizilized函数中,这样我们就可以确保代码已准备就绪
测试代码!
此时,您可以测试代码:
node main.js
控制台中的输出将为:
node main.js
Hello world!!!
Hello {}
something
示例代码
您可以在此存储库中测试代码
使用Emscripten编写代码很开心 使用Emscripten公开类
如果你想用EnScript来公开C++类,你需要这些步骤,编写C++代码:
// hello_world.cpp
#include <iostream>
class Hello {
public:
Hello() {
std::cout << "Hello world!!!\n" << std::endl;
}
void saySomething() {
std::cout << "something" << std::endl;
}
};
#include <emscripten/bind.h>
using namespace emscripten;
EMSCRIPTEN_BINDINGS(jshello) {
class_<Hello>("Hello")
.constructor<>()
.function("saySomething", &Hello::saySomething);
}
我们将代码包装到OnRuntimeInitizilized函数中,这样我们就可以确保代码已准备就绪
测试代码!
此时,您可以测试代码:
node main.js
控制台中的输出将为:
node main.js
Hello world!!!
Hello {}
something
示例代码
您可以在此存储库中测试代码
使用Emscripten编写代码很开心 我刚试过,但我得到了这个错误
home/kubuntu/Desktop/c++/hello\u world/hello\u world.js:117^
我刚试过,但我遇到了这个错误home/kubuntu/Desktop/c++/hello\u world/hello\u world.js:117^代码>您的代码中存在一些错误和不一致,这些错误和不一致将导致代码失败。我将发布一个答案以更好地解释。我应该更好地解释什么?不,我指的是我,请参阅我的答案。您的代码中存在一些错误和不一致,这将使您的代码失败。我将发布一个答案,以便更好地解释。我应该更好地解释什么?不,我指的是我,请参阅我的答案。