Ask Your Question
1

Can't use subscribe from inside c++ class

asked 2012-11-02 09:46:40 -0600

TiagoRibeiro gravatar image

updated 2014-01-28 17:14:08 -0600

ngrennan gravatar image

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.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
4

answered 2012-11-02 12:35:55 -0600

joq gravatar image

updated 2012-11-02 18:07:53 -0600

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.

edit flag offensive delete link more

Comments

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.

TiagoRibeiro gravatar imageTiagoRibeiro ( 2012-11-07 03:39:12 -0600 )edit
1

answered 2012-11-02 14:11:46 -0600

TiagoRibeiro gravatar image

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!

edit flag offensive delete link more

Comments

Don't have enough points to accept my own answer, but this was the solution.

TiagoRibeiro gravatar imageTiagoRibeiro ( 2012-11-02 14:12:39 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-11-02 09:46:40 -0600

Seen: 1,068 times

Last updated: Nov 02 '12