类问题(c++ 和序言)

发布于 2024-08-29 11:14:03 字数 1632 浏览 8 评论 0原文

我正在使用 Prolog 的 C++ 接口(SWI-cpp.h 的类和方法)。为了制定约翰喜欢玛丽、艾玛和萨拉的简单回溯:

likes(john, mary).
likes(john, emma).
likes(john, ashley).

我可以这样做:

{
  PlFrame fr;
  PlTermv av(2);
  av[0] = PlCompound("john");
  PlQuery q("likes", av);
  while (q.next_solution())
  {
   cout << (char*)av[1] << endl;
  }
}

这在单独的代码中工作,因此语法是正确的。但我也在努力得到这个 在类中进行简单的回溯:

class UserTaskProlog
{
  public:
                UserTaskProlog(ArRobot* r);
                ~UserTaskProlog();
  protected:
                int cycles;
                char* argv[1];
                ArRobot* robot;
                void logTask();
};

该类工作正常,我的周期变量会增加每个机器人周期。但是,当我运行主代码时,我收到一条未处理的异常错误消息:

UserTaskProlog::UserTaskProlog(ArRobot* r) : robotTaskFunc(this, &UserTaskProlog::logTask)
{
  cycles = 0;
  PlEngine e(argv[0]);
  PlCall("consult('myFile.pl')");
  robot->addSensorInterpTask("UserTaskProlog", 50, &robotTaskFunc);
}

UserTaskProlog::~UserTaskProlog()
{
  robot->remSensorInterpTask(&robotTaskFunc);
  // Do I need a destructor here for pl?
}

void UserTaskProlog::logTask()
{
  cycles++;
  cout << cycles;
  {
    PlFrame fr;
    PlTermv av(2);
    av[0] = PlCompound("john");
    PlQuery q("likes", av);
    while (q.next_solution())
    {
     cout << (char*)av[1] << endl;
    }
  }
}

我有 PlFrame 的左括号和右括号。我有我的框架、我的查询等等...回溯并打印出玛丽、艾玛和莎拉的完全相同的代码。我收到错误消息时缺少什么?

我认为代码应该这样做:我希望每次循环增量时,玛丽、艾玛和莎拉都会打印一次。但是,它会自动打开 SWI-cpp.h 文件并指向 PlFrame 类。它想告诉我什么?我没有发现我的 PlFrame 类声明有任何问题。

谢谢,

I am using the C++ interface to Prolog (the classes and methods of SWI-cpp.h). For working out a simple backtracking that john likes mary and emma and sara:

likes(john, mary).
likes(john, emma).
likes(john, ashley).

I can just do:

{
  PlFrame fr;
  PlTermv av(2);
  av[0] = PlCompound("john");
  PlQuery q("likes", av);
  while (q.next_solution())
  {
   cout << (char*)av[1] << endl;
  }
}

This works in a separate code, so the syntax is correct. But I am also trying to get this
simple backtracking to work within a class:

class UserTaskProlog
{
  public:
                UserTaskProlog(ArRobot* r);
                ~UserTaskProlog();
  protected:
                int cycles;
                char* argv[1];
                ArRobot* robot;
                void logTask();
};

This class works fine, with my cycles variable incrementing every robot cycle. However, when I run my main code, I get an Unhandled Exception error message:

UserTaskProlog::UserTaskProlog(ArRobot* r) : robotTaskFunc(this, &UserTaskProlog::logTask)
{
  cycles = 0;
  PlEngine e(argv[0]);
  PlCall("consult('myFile.pl')");
  robot->addSensorInterpTask("UserTaskProlog", 50, &robotTaskFunc);
}

UserTaskProlog::~UserTaskProlog()
{
  robot->remSensorInterpTask(&robotTaskFunc);
  // Do I need a destructor here for pl?
}

void UserTaskProlog::logTask()
{
  cycles++;
  cout << cycles;
  {
    PlFrame fr;
    PlTermv av(2);
    av[0] = PlCompound("john");
    PlQuery q("likes", av);
    while (q.next_solution())
    {
     cout << (char*)av[1] << endl;
    }
  }
}

I have my opening and closing brackets for PlFrame. I have my frame, my query, etc... The exact same code that backtracks and prints out mary and emma and sara. What am I missing here that I get an error message?

Here is what I think the code should do: I expect mary and emma and sara to be printed out once, every time cycles increments. However, it opens SWI-cpp.h file automatically and points to class PlFrame. What is it trying to tell me? I don't see anything wrong with my PlFrame class declaration.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽蝶幻影 2024-09-05 11:14:03

您不能像这样将指针传递给实例方法,您可能必须创建 C 函数包装器才能传递给 addSensorInterpTask()。这似乎是问题的根源,因为机器人使用不正确的参数调用该方法。另外,您应该捕获 PlException 并检查它告诉您的内容。
不管怎样,你的例子不能按原样编译,因为它不完整(什么是ArRobot?)并且不精确(ashley vs sara)。在提交问题之前,请尝试想象其他人如何能够轻松地重现您的问题。

You cannot pass pointers to instance methods like this, you probably have to create C function wrapper for passing to addSensorInterpTask(). This seems to be the root of your problem, as robot calls the method with incorrect parameters. Also, you should catch PlException and check what it tells you.
Anyway, your example cannot be compiled as it is, since it is incomplete (what is ArRobot?) and imprecise (ashley v.s. sara). Please try to imagine how other people could reproduce your problem without much a of a hassle before submitting a question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文