开发者

Class Problem (c++ and prolog)

开发者 https://www.devze.com 2022-12-27 23:54 出处:网络
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:

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 erro开发者_运维百科r 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,


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消