Unable to Add New Class in Argos2 Existing Code

Requests regarding how to set up experiments in ARGoS.
Waqar731

Unable to Add New Class in Argos2 Existing Code

Postby Waqar731 » Sun Jan 25, 2015 10:05 pm

Hi

I have made a class for storing the information of visitied arena. I run that class separtate on ubuntu it compiles and run successfully.
But when I am trying to add this new class in my existing code of Argos2. But i get error when i use the 'make' command to compile all files.

Code of class is given below:

Code: Select all

#include <iostream> class CCI_Point { private: double x; double y; public: CCI_Point() { x = 0.0; y = 0.0; } CCI_Point (double x, double y) { this->x = x; this->y = y; } bool operator==(const CCI_Point &that) const { return (x == that.x && y == that.y); } double getX() const { return this->x; } double getY() const { return this->y; } }; namespace std { template <> class std::hash<CCI_Point> { public: inline size_t operator()(const CCI_Point &p) const { hash<double> double_hasher; return double_hasher(p.getX()) ^ double_hasher(p.getY()); } }; };
I got the error given below

Code: Select all

In file included from /home/waqar/argos2-examples/controllers/distance_scannar/distance_scannar.h:26:0, from /home/waqar/argos2-examples/controllers/distance_scannar/distance_scannar.cpp:1: /usr/include/argos2/common/control_interface/ci_point.h:36:25: error: ‘hash’ is not a class template template <> class std::hash<CCI_Point> ^ /usr/include/argos2/common/control_interface/ci_point.h:37:2: error: qualified name does not name a class before ‘{’ token { ^

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

Re: Unable to Add New Class in Argos2 Existing Code

Postby pincy » Mon Jan 26, 2015 5:58 am

Hi,

The problem you are encountering is not related to ARGoS. Rather, it's a syntax error in the use of templates.

You're trying to redefine the class std::hash<>, which you can't do because the class is already defined.

If you want to use the class std::hash<> with your class CCI_Point, you just need to write

Code: Select all

std::hash<CCI_Point> my_point_hash;
See also http://en.cppreference.com/w/cpp/utility/hash.

Cheers,
Carlo
I made ARGoS.

Waqar731

Re: Unable to Add New Class in Argos2 Existing Code

Postby Waqar731 » Mon Jan 26, 2015 9:08 am

Hi,
Actually when i compile and run this code separately from argos code then it gives 100% correct result but when i tried using argos then it gives error.
However, now i have even update the code as you suggest, but still having a problem.

Code is given below:

Code: Select all

#include <iostream> class CCI_Point { private: double x; double y; public: CCI_Point() { x = 0.0; y = 0.0; } CCI_Point (double x, double y) { this->x = x; this->y = y; } bool operator==(const CCI_Point &that) const { return (x == that.x && y == that.y); } double getX() const { return this->x; } double getY() const { return this->y; } }; namespace std { std::hash<CCI_Point> my_point_hash; };
Error is given below:

Code: Select all

In file included from /home/waqar/argos2-examples/controllers/distance_scannar/distance_scannar.h:26:0, from /home/waqar/argos2-examples/controllers/distance_scannar/distance_scannar.cpp:1: /usr/include/argos2/common/control_interface/ci_point.h:37:2: error: ‘hash’ in namespace ‘std’ does not name a type std::hash<CCI_Point> my_point_hash; ^
For example:

http://prateekvjoshi.com/2014/06/05/usi ... d-classes/





The main puurpose of doing is that i want to store the x and y point in hash table. First i implement this algorithm outside the argos simulator it gives correct result. The code is given below

Code: Select all

#include <iostream> #include <unordered_set> #include <vector> class Point { private: double x; double y; public: Point() { x = 0.0; y = 0.0; } Point (double x, double y) { this->x = x; this->y = y; } bool operator==(const Point &that) const { return (x == that.x && y == that.y); } double getX() const { return this->x; } double getY() const { return this->y; } }; namespace std { template <> class hash<Point> { public: inline size_t operator()(const Point &p) const { hash<double> double_hasher; return double_hasher(p.getX()) ^ double_hasher(p.getY()); } }; }; using namespace std; int main(int argc, char **argv) { std::unordered_set<Point> points_visited; std::vector< std::vector<Point> > arena; arena.resize(5); for(int i=0; i < 5; i++) { arena[i].resize(5); } for(int i = 0; i < 5; i++) { for(int j=0; j < 5; j++) { arena[i][j] = Point((double)i, (double)j); if(i == j) { points_visited.insert(arena[i][j]); } } } points_visited.insert(arena[2][3]); for(int i = 0; i < 5; i++) { for(int j = 0; j < 5; j++) { //cout only the points that were not visited. auto got = points_visited.find(arena[i][j]); if(got == points_visited.end()) { std::cout << arena[i][j].getX() << " " << arena[i][j].getY() << std::endl; } else { cout << "Coordinates (" << i << ", " << j << ")" <<" has already been visited" << endl; } } } }
But when i try to use same code in argos2 simulator in a (ci_point.h) header file. Then i am getting the error.

What 's the difference between simple compiler and argos2 compiler?
What is the reason behind this error?
How to solve this bug?

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

Re: Unable to Add New Class in Argos2 Existing Code

Postby pincy » Mon Jan 26, 2015 2:13 pm

Hi,

Again, this is not an issue with ARGoS.
The main puurpose of doing is that i want to store the x and y point in hash table.
If you want to store your point class in a hash map, you could use the map definition provided by the standard library: http://www.cplusplus.com/reference/map/map/. That would be the simplest option.
I don't understand exactly why you're linking that blog post, which talks about a different topic: custom hashing rather than hash maps.

The code in your original request and the one you say is working differ substantially. In particular:
  1. In your original request you wrote "std::hash" (which is wrong), while in the code you report as working, you wrote just "hash" (which is right);
  2. In your original request the error refers to std::hash not being a template (that's because you're probably missing an #include), while in the code you report as working the #include statements are in place.
Cheers,
Carlo
I made ARGoS.

Waqar731

Re: Unable to Add New Class in Argos2 Existing Code

Postby Waqar731 » Mon Jan 26, 2015 3:27 pm

Hi,
"how to activate C++11" in builld in Argos2?


Return to “How to... ?”