ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

main function doesn't find functions of my class: ROS or c++ error?

asked 2012-12-05 04:14:14 -0500

ubisum gravatar image

updated 2014-04-20 14:09:50 -0500

ngrennan gravatar image

hello everyone
I wrote my class structure in a header file (stage_listener.h):

#ifndef sl_H
#define sl_H

#include "ros/ros.h"
#include "nav_msgs/Odometry.h"
#include "geometry_msgs/Pose.h"
#include "geometry_msgs/Point.h"
#include "stdio.h"
#include "sensor_msgs/LaserScan.h"
#include "list"
#include "vector"
#include "scan_node.h"
#include "odom_node.h"
#include "coord.h"

class stage_listener{
public:
    stage_listener(){};
    int numOdom();
    int numScan();

private:
    std::list<odom_node> odom_list;
    std::list<scan_node> scan_list;
    std::list<coord> corners_list;

    std::list<coord> polar2cart(std::vector<float>, float, float, float, float);
    void addOdomNode (const nav_msgs::Odometry);
    void addScanNode (const sensor_msgs::LaserScan);
    void extractCorners(std::vector<float>, float, float, float, float);
    int distance (float, float, float, float, float);
    void nodes2text(std::vector<odom_node>, std::vector<scan_node>);

};  


#endif

Then I wrote its associated .cpp file (stage_listener.cpp):

#include "ros/ros.h"
#include "nav_msgs/Odometry.h"
#include "geometry_msgs/Pose.h"
#include "geometry_msgs/Point.h"
#include "stdio.h"
#include "sensor_msgs/LaserScan.h"
#include "stage_listener.h"
#include "odom_node.h"
#include "scan_node.h"
#include "tf/transform_listener.h"
#include "list"
#include "vector"
#include "coord.h"
#include "math.h"
#include "stdlib.h"
#include "iterator"
#include "string"


stage_listener::stage_listener(){}

int stage_listener::numOdom(){
    return (int)(odom_list.size());
}

int stage_listener::numScan(){
    return (int)(scan_list.size());
}

void stage_listener::addOdomNode (const nav_msgs::Odometry mes){
    geometry_msgs::Pose robot_pose = mes.pose.pose;
    geometry_msgs::Point robot_point = robot_pose.position;

    odom_node *on = new odom_node();
    (*on).xCoord = robot_point.x;
    (*on).yCoord = robot_point.y;
    (*on).frame_id = mes.header.frame_id;

    double orientation = tf::getYaw(robot_pose.orientation);
    (*on).angle = (float)orientation;

    odom_list.push_back(*on);
}

std::list<coord> stage_listener::polar2cart(std::vector<float> v, float a_min, float a_inc, float range_min, float range_max){
    std::list<coord> lc;
    for(int i=0; i<(int)v.size(); i++){
        if(v[i]>= range_min && v[i]<=range_max){
            coord *c = new coord();
            (*c).x = v[i]*cos(a_min + a_inc*i);
            (*c).y = v[i]*sin(a_min + a_inc*i);
            lc.push_back(*c);
        }
    }

    return lc;
}

void stage_listener::addScanNode (const sensor_msgs::LaserScan mes){
    scan_node *sn = new scan_node();
    (*sn).scan = mes.ranges;
    (*sn).frame_id = mes.header.frame_id;
    (*sn).cartesian = polar2cart(mes.ranges, mes.angle_min, mes.angle_increment, mes.range_min, mes.range_max);

    scan_list.push_back(*sn);
}

int distance (float a, float b, float c, float x0, float y0){
    return abs(a*x0 + b*y0 + c)/sqrt(pow(a,2) + pow(b,2));
}

void extractCorners(std::list<coord> points, float d_max, std::list<coord> dest){
    if(points.size() == 1){
        coord *corner = new coord();
        (*corner).x = points.front().x;
        (*corner).y = points.front().y;
        dest.push_back(*corner);
    }

    else if(points.size() == 2){
        coord *corner_a = new coord();
        coord *corner_b = new coord();

        (*corner_a).x = points.front().x;
        (*corner_a).y = points.front().y;


        (*corner_b).x = points.back().x;
        (*corner_b).y = points.back().y;

        dest.push_back(*corner_a);
        dest.push_back(*corner_b);
    }

    else{
        std::list<coord>::iterator i = points.end();
        float x1 = points.front().x;
        float y1 = points.front().y;
        float x2 = (*i).x;
        float y2 = (*i).y;

        float a = y1-y2;
        float b = x2-x1;
        float c = y1*(x1-x2) + x1*(y2-y1);

        int max = 0;
        int ref = 0;
        int count = points.size()-2;
        i--;

        while(i!=points.begin()){
            coord temp = (*i);
            int ...
(more)
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2012-12-05 04:49:46 -0500

dornhege gravatar image

Your CMake states that you will build two independent nodes.

You want the listener.cpp to be build into state_main, not into a separate listener executable, so add src/stage_listener.cpp to the stage_main line and remove the listener entry.

edit flag offensive delete link more
1

answered 2012-12-05 04:46:31 -0500

joq gravatar image

That is a linker error. You compiled stage_main without a copy of listener.cpp.

Try this in your CMakeLists.txt:

rosbuild_add_executable(stage_main src/stage_main.cpp src/listener.cpp)

A better solution would involve creating a listener shared library which stage_main could reference.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-12-05 04:14:14 -0500

Seen: 1,587 times

Last updated: Dec 05 '12