Redefinition of class error when including a header file
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{ public: odom_node(); //~odom_node(); float xCoord; float yCoord; float angle; std::string frame_id; };
And this is the associated .cpp file:
#include "odom_node.h" odom_node::odom_node(){ }
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 :)
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()"
the error appears when i write "odom_node on;" in stage_listener.cpp
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++.