Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Having trouble sending a message to ROS

I am pretty new in ROS. I am just trying to publish a message to a node in a linux server with this code:

#include "stdafx.h"
#include "ros.h"
#include <string>
#include <stdio.h>
#include <Windows.h>

using std::string;

int _tmain(int argc, _TCHAR * argv[])
{
    ros::NodeHandle nh;
    char *ros_master = "*.*.*.*";

    printf("Connecting to server at %s\n", ros_master);
    nh.initNode(ros_master);

    printf("Advertising cmd_vel message\n");
    string sent = "Hello robot";
    ros::Publisher cmd_vel_pub("try", sent);
    nh.advertise(cmd_vel_pub);

    printf("All done!\n");
    return 0;
}

The compiler gives me these errors:

 Error  C2664 'ros::Publisher::Publisher(ros::Publisher &&)': cannot convert argument 2 from 'std::string' to 'ros::Msg *'  LeapMotion     c:\users\vive-vr-pc\documents\visual studio    2015\projects\leapmotion\leapmotion\leapmotion.cpp    22  
Error (active)  no instance of constructor "ros::Publisher::Publisher" matches the argument list    LeapMotion  c:\Users\Vive-VR-PC\Documents\Visual Studio 2015\Projects\LeapMotion\LeapMotion\LeapMotion.cpp  22

I am on Visual Studio 2015 and there aren't a lot of tutorial from windows to linux, so I am confused on what to do. Many thanks for the help! :D

Having trouble sending a message to ROS

I am pretty new in ROS. I am just trying to publish a message to a node in a linux server with this code:

#include "stdafx.h"
#include "ros.h"
#include <string>
#include <stdio.h>
#include <Windows.h>

using std::string;

int _tmain(int argc, _TCHAR * argv[])
{
    ros::NodeHandle nh;
    char *ros_master = "*.*.*.*";

    printf("Connecting to server at %s\n", ros_master);
    nh.initNode(ros_master);

    printf("Advertising cmd_vel message\n");
    string sent = "Hello robot";
    ros::Publisher cmd_vel_pub("try", sent);
    nh.advertise(cmd_vel_pub);

    printf("All done!\n");
    return 0;
}

The compiler gives me these errors:

 Error  C2664 'ros::Publisher::Publisher(ros::Publisher &&)': cannot convert argument 2 from 'std::string' to 'ros::Msg *'  LeapMotion     c:\users\vive-vr-pc\documents\visual studio    2015\projects\leapmotion\leapmotion\leapmotion.cpp    22  
Error (active)  no instance of constructor "ros::Publisher::Publisher" matches the argument list    LeapMotion  c:\Users\Vive-VR-PC\Documents\Visual Studio 2015\Projects\LeapMotion\LeapMotion\LeapMotion.cpp  22

I am on Visual Studio 2015 and there aren't a lot of tutorial from windows to linux, so I am confused on what to do. Many thanks for the help! :D


Edit: Thanks for the clarity.

So I have modified my code in this way:

#include "stdafx.h"
#include "ros.h"
#include <string>
#include <stdio.h>
#include <Windows.h>
#include <std_msgs\String.h>

int _tmain(int argc, _TCHAR * argv[])
{
    ros::NodeHandle nh;
    char *ros_master = "*.*.*.*";

    printf("Connecting to server at %s\n", ros_master);
    nh.initNode(ros_master);

    printf("Advertising cmd_vel message\n");
    std_msgs::String sent;
    ros::Publisher cmd_vel_pub("try", &sent);
    nh.advertise(cmd_vel_pub);
    while (1) {
        sent.data = "Hello robot";
        cmd_vel_pub.publish(&sent);
        nh.spinOnce();
        Sleep(10);
    }
    printf("All done!\n");
    return 0;
}

But it has not builded yet. Now I have errors related to Windows socket or stuff like that.

1>------ Build started: Project: LeapMotion, Configuration: Debug Win32 ------
1>  LeapMotion.cpp
1>LeapMotion.obj : error LNK2019: unresolved external symbol "public: __thiscall WindowsSocket::WindowsSocket(void)" (??0WindowsSocket@@QAE@XZ) referenced in function "public: __thiscall ros::NodeHandle_<class WindowsSocket,25,25,512,512>::NodeHandle_<class WindowsSocket,25,25,512,512>(void)" (??0?$NodeHandle_@VWindowsSocket@@$0BJ@$0BJ@$0CAA@$0CAA@@ros@@QAE@XZ)
1>LeapMotion.obj : error LNK2019: unresolved external symbol "public: void __thiscall WindowsSocket::init(char *)" (?init@WindowsSocket@@QAEXPAD@Z) referenced in function "public: void __thiscall ros::NodeHandle_<class WindowsSocket,25,25,512,512>::initNode(char *)" (?initNode@?$NodeHandle_@VWindowsSocket@@$0BJ@$0BJ@$0CAA@$0CAA@@ros@@QAEXPAD@Z)
1>LeapMotion.obj : error LNK2019: unresolved external symbol "public: int __thiscall WindowsSocket::read(void)" (?read@WindowsSocket@@QAEHXZ) referenced in function "public: virtual int __thiscall ros::NodeHandle_<class WindowsSocket,25,25,512,512>::spinOnce(void)" (?spinOnce@?$NodeHandle_@VWindowsSocket@@$0BJ@$0BJ@$0CAA@$0CAA@@ros@@UAEHXZ)
1>LeapMotion.obj : error LNK2019: unresolved external symbol "public: void __thiscall WindowsSocket::write(unsigned char const *,int)" (?write@WindowsSocket@@QAEXPBEH@Z) referenced in function "public: virtual int __thiscall ros::NodeHandle_<class WindowsSocket,25,25,512,512>::publish(int,class ros::Msg const *)" (?publish@?$NodeHandle_@VWindowsSocket@@$0BJ@$0BJ@$0CAA@$0CAA@@ros@@UAEHHPBVMsg@2@@Z)
1>LeapMotion.obj : error LNK2019: unresolved external symbol "public: unsigned long __thiscall WindowsSocket::time(void)" (?time@WindowsSocket@@QAEKXZ) referenced in function "public: void __thiscall ros::NodeHandle_<class WindowsSocket,25,25,512,512>::requestSyncTime(void)" (?requestSyncTime@?$NodeHandle_@VWindowsSocket@@$0BJ@$0BJ@$0CAA@$0CAA@@ros@@QAEXXZ)
1>LeapMotion.obj : error LNK2019: unresolved external symbol "void __cdecl ros::normalizeSecNSec(unsigned int &,unsigned int &)" (?normalizeSecNSec@ros@@YAXAAI0@Z) referenced in function "public: void __thiscall ros::NodeHandle_<class WindowsSocket,25,25,512,512>::setNow(class ros::Time &)" (?setNow@?$NodeHandle_@VWindowsSocket@@$0BJ@$0BJ@$0CAA@$0CAA@@ros@@QAEXAAVTime@2@@Z)
1>C:\Users\Vive-VR-PC\Documents\Visual Studio 2015\Projects\LeapMotion\Debug\LeapMotion.exe : fatal error LNK1120: 6 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Can't understand why. I have included all the libraries I need to.