Std:badcast

Requests regarding how to set up experiments in ARGoS.
Abdu Sulaiman
Posts: 2
Joined: Mon Apr 12, 2021 5:50 pm

Std:badcast

Postby Abdu Sulaiman » Mon Apr 12, 2021 6:08 pm

Hello, I am a student working on a foraging behavior on Argos. However, when I try to run an experiment, the terminal returns std:badcast. When I searched for the problem, the problem could be because I am using two different controllers in my XML file.
[
problem.JPG
problem.JPG (19.17 KiB) Viewed 7326 times
I have seen this problem before, the solution that was suggested is to cast to pointer and not to reference. I don't particularly know how to do that. I really hope someone can show me how. Forgive me for my lack of knowledge.

pincy
Site Admin
Posts: 632
Joined: Thu Mar 08, 2012 8:04 pm
Location: Boston, MA
Contact:

Re: Std:badcast

Postby pincy » Mon Apr 12, 2021 6:33 pm

From the information you provide, it's not possible to suggest you a specific fix. I will give you some general guidelines.

In C++ there are pointers and references. Make sure to read about the difference between them, but the thing that matters is that pointers can be null and references cannot.

The second thing to know is that dynamic_cast<type>(object) might fail when object cannot be transformed into type. If you use a pointer, dynamic_cast<>() returns a null value, which you can later check. If you use a reference, dynamic_cast<>() cannot return null (references cannot be null!), so C++ raises an exception called std::bad_cast.

This issue has nothing to do specifically with ARGoS, it's just a C++ thing. A possible approach, without knowing your code, is to proceed as follows:

Code: Select all

CMyController1* c1 = dynamic_cast<CMyController1*>(&footbot.GetControllableEntity().GetController()); CMyController2* c2 = dynamic_cast<CMyController2*>(&footbot.GetControllableEntity().GetController()); if(c1 != nullptr) { ... the controller is of type CMyController1 } if(c2 != nullptr) { ... the controller is of type CMyController2 }
I made ARGoS.

Abdu Sulaiman
Posts: 2
Joined: Mon Apr 12, 2021 5:50 pm

Re: Std:badcast

Postby Abdu Sulaiman » Mon Apr 12, 2021 6:55 pm

Thank so much!! This is exactly what I needed.


Return to “How to... ?”