【发布时间】:2019-07-03 04:41:51
【问题描述】:
我正在使用实现 minimax tic-tac-toe AI 的 v8 编写一个 c++ NodeJs 本机插件。
我遇到了嵌套函数不起作用的问题。
这是我的代码:
namespace Game {
Move bestMove(...) {
// implementation
}
}
namespace addon {
using namespace v8;
using std::vector;
...
// this function returns the best move back to nodejs
void bestMove(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
...
auto returnVal = Game::bestMove(params); // Game::bestMove() returns the best move for the computer
args.GetReturnValue().Set((returnVal.row * 3) + returnVal.col); // returns the move back to nodejs
}
通常,如果游戏板是这样的(电脑是o):
x _ _
_ _ _
_ _ _
该函数不应返回 0,因为它已被 x 占用。
但是它似乎总是返回0。
经过一番调查,我意识到函数Game::bestMove() 永远不会被调用。
添加是的,我知道这是问题所在,因为在函数Move bestMove() 中添加std::cout << "Computing"; 后,它从未打印到控制台。
但是,如果我在函数addon::bestMove() 中添加std::cout << "Computing";,它会起作用。
也没有抛出编译时错误。
感谢您的帮助。
【问题讨论】:
标签: c++ node.js function add-on node.js-addon