ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Redefinition of class error when including a header file

asked 2012-11-19 21:27:51 -0500

ubisum gravatar image

hi you all. I'm trying to implement a list of odometry measurements and I defined nodes in .h and .cpp files. Then, in a client class Stage_listener.cpp(having an associated .h file that, I guess, it's not useful to include here), I try to create a node :

#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 "tf/transform_listener.h"

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;

    //float xCoord = robot_point.x;
    //float yCoord = robot_point.y;
    //float zCoord = robot_point.z;

    odom_node on;
    //on.xCoord = robot_point.x;
    //double orientation = tf::getYaw(robot_pose.orientation);

int main(){}

Here are the two files I used to define the odometric nodes. This is the header:

#include "ros/ros.h"

class odom_node{
    float xCoord;
    float yCoord;
    float angle;
    std::string frame_id;

And this is the associated .cpp file:

#include "odom_node.h"


The latter is very simple since there are no functions to be implemented; now I get an error:

[100%] Building CXX object CMakeFiles/stage_listener.dir/src/stage_listener.o
In file included from /home/ubisum/fuerte_workspace/beginner/src/stage_listener.cpp:8:0:
/home/ubisum/fuerte_workspace/beginner/src/odom_node.h:3:7: error: redefinition of ‘class odom_node’
/home/ubisum/fuerte_workspace/beginner/src/odom_node.h:3:7: error: previous definition of ‘class odom_node’

I can't get rid of it. Do you have any idea? Thanks for your help and sorry for my boring ROS/C++ newbie questions :)

edit retag flag offensive close merge delete


thanks for your suggestion, Lorenz sadly, i was given a project and c++ at the same time and expiration date for delivery forces not to dedicate much time to learning deeply c++

anyway, i had added in meanwhile #ifndef etc... now i get a new error: "undefined reference to `odom_node::odom_node()"

ubisum gravatar image ubisum  ( 2012-11-19 23:07:02 -0500 )edit

the error appears when i write "odom_node on;" in stage_listener.cpp

ubisum gravatar image ubisum  ( 2012-11-19 23:07:53 -0500 )edit

The problem now is the same as in your previous question. You are not linking together all your C++ files. I don't think you will be able to solve your assignment until you learn C++.

Lorenz gravatar image Lorenz  ( 2012-11-20 00:13:33 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2012-11-19 23:01:30 -0500

Lorenz gravatar image

You are probably missing the #ifndef guards in your header file. In C++ you need to add:

#ifndef FOO_H
#define FOO_H

... header file contents ...


I really suggest that you dive into C++ a little more before continuing with ROS. A solid knowledge of that language will save you a lot of time.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


Asked: 2012-11-19 21:27:51 -0500

Seen: 9,038 times

Last updated: Nov 19 '12