gpt4 book ai didi

c++ - QScript 的问题

转载 作者:太空狗 更新时间:2023-10-29 23:20:09 25 4
gpt4 key购买 nike

我整天都在努力让这段代码正常工作。它应该与 QScript help page 中提供的代码相同但不幸的是,它根本不起作用!

class Person
{
public:
QString nm;

Person()
{

}

Person(QString& name)
:nm(name)
{

}
};

Q_DECLARE_METATYPE(Person)
Q_DECLARE_METATYPE(Person*)

QScriptValue Person_ctor(QScriptContext* c,QScriptEngine* e)
{
QString x = c->argument(0).toString();
return e->toScriptValue(Person(x));
}

QScriptValue Person_prototype_toString(QScriptContext* c,QScriptEngine* e)
{
Person* per = qscriptvalue_cast(c->thisObject());
qDebug(qPrintable(per->nm));
return e->undefinedValue();
}


....
QScriptValue per_ctr = eng->newFunction(Person_ctor);
per_ctr.property("prototype").setProperty("toString",eng->newFunction(Person_prototype_toString));
per_ctr.property("prototype").setProperty("myPrint",eng->newFunction(Person_prototype_toString));
eng->globalObject().setProperty("Person",per_ctr);
...

如果我尝试在 JavaScript 中评估以下代码

var p = new Person("Guido");
p.toString();
p.myPrint();

我应该得到:

Guido
Guido

相反,我真正获得的是来自 toString 函数的白色字符串(可能正在调用 Object.toString 函数)和“解释器错误:第 2 行:TypeError:表达式 'p.myPrint' [undefined] 的结果不是一个功能。”来自 myPrint 的错误消息。我想我没有将这两个函数正确地连接到 Person 原型(prototype),即使我试图完全遵循文档页面......请有人解释我的错是什么?!?谢谢!

最佳答案

如果去掉 toString 和 myPrint 之后的括号会怎样?

关于c++ - QScript 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3548222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com