0
$\begingroup$

Rosanswers logo

I am having trouble getting ros inside a c++ class. Running Windows 7, compiling with Visual Studio 2010.

I started by trying to adapt the "listener" project in rosws, which contains a visualstudio example that compiles and runs ok.

However, even if I simply put everything inside a class, I get this error:

Error   30  error C2660: 'ros::NodeHandle::subscribe' : function does not take 3 arguments  C:\opt\rosws\fuerte\sdk-tutorials\listener\listener.cpp 59  1   listener

This simple example replicates the problem:

#include "stdafx.h"
#include <iostream>
#include <ros/ros.h>
#include <std_msgs/String.h>
#include <sstream>

class listener {
    public:
    void chatterCallback(const std_msgs::String::ConstPtr& msg)
    {
        ROS_INFO("I heard: [%s]", msg->data.c_str());
    }

    int mymain(int argc, char** argv)
    {
        ros::init(argc, argv, "listener");
        ros::NodeHandle n;
        ros::Subscriber sub = n.subscribe("chatter", 1000, &listener::chatterCallback);
        ros::spin();

        return 0;
    }
};

So you see that I am just sticking everything inside a class so that I can create and manipulate instances of my objects.

Why does the compiler not find the correct subscribe function now? If I just compile the code without the class, it works.

I checked out the "talker" project which used an "advertise" function, but that one works ok if I stick it inside a class.


Originally posted by TiagoRibeiro on ROS Answers with karma: 41 on 2012-11-02

Post score: 1

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

You need to pass the class pointer along with the callback:

ros::Subscriber sub = n.subscribe("chatter", 1000, &listener::chatterCallback, this);

EDIT: I missed the fact that you are compiling on Windows. I have no idea if that interface is officially supported on VS2010.

Your work-around is not really a solution. If you actually want to use ROS on Windows, please open a defect ticket so the maintainers know their code does not work with your compiler.


Originally posted by joq with karma: 25443 on 2012-11-02

This answer was ACCEPTED on the original site

Post score: 5


Original comments

Comment by TiagoRibeiro on 2012-11-07:
You are right. Can you please direct me to where I should post the ticket? I found the ticket handoffs but didn't quite understand where I should post this as a ticket.

Comment by joq on 2012-11-07:
Sure: https://code.ros.org/trac/ros/newticket?component=ros_comm&type=defect&&ros_comm

$\endgroup$
0
$\begingroup$

Rosanswers logo

I found that while looking through the node_handle class code, but I still got the same error. It was like of the compiler was just trying to match the parameters with the 1-parameter version that takes in only a SubscribeOptions object.

Actually I ended up added the following:

template<class M, class C> Subscriber subscribe(const std::string& topic, uint32_t queue_size, void(*fp)(const boost::shared_ptr<M const>&));

to the "ros\fuerte\x86\include\ros\node_handle.h" file, right after to line 785 which contains:

Subscriber subscribe(SubscribeOptions& ops);

I haven't messed around with C++ for some years now, been mainly working in C# so I'm not really sure what is the issue, it just seemed to me that if I was inside another class/namespace, the compiler wasn't finding all the versions of the function because they are all templated, and just found this one on line 785, so I tried replicating it for the version I wanted.. It compiled correctly and is now working.

Interestingly, I was still forced to add the class pointer parameter, as without it, the compiler produces the same error.

Maybe I should take some lessons on templates, because right now this just seems like creepy stuff to me :)

Anyway, problem solved. Thanks for the support!


Originally posted by TiagoRibeiro with karma: 41 on 2012-11-02

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by TiagoRibeiro on 2012-11-02:
Don't have enough points to accept my own answer, but this was the solution.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.