Undefined reference to class::function()
Hi everybody, I'm a little bit new to ROS and making classes in c++ so excuse me for all the crap I'll surely write. My problem is that: I want to write a class called Cao and call its function from the Ros file which contains the main, but when I try to catkin_make it gives me the error: undefined reference to `Cao::SayHello()'.
Since the absence of a main I've declared the class a library adding this line in the CMakeLists.txt:
add_library(quadcopter_ctrl src/Cao.cpp)
The header Cao.h looks like this:
#ifndef CAO_H_
#define CAO_H_
#include <iostream>
class Cao{
private:
int state;
public:
Cao();
virtual ~Cao();
static void SayHello();
};
#endif /* CAO_H_ */
And the Cao.cpp is like this:
#include "Cao.h"
Cao::Cao(){
state = 0;
}
Cao::~Cao(){
// TODO Auto-generated destructor stub
}
void Cao::SayHello(){
std::cout << "Hello World!";
std::cout << std::endl;
}
I'm calling the SayHello function in this quadcopterRosCtrl.cpp file:
#include "ros/ros.h"
#include "geometry_msgs/PoseStamped.h"
#include "std_msgs/String.h"
#include "../include/quadcopter_ctrl/termColors.h"
#include "../include/quadcopter_ctrl/quadcopterRosCtrl.h"
#include "Cao.h"
geometry_msgs::PoseStamped quadPos;
int quadPosAcquired = 0;
void quadPosFromVrep(const geometry_msgs::PoseStamped::ConstPtr& pubQuadPose)
{
quadPos.pose.position.x = pubQuadPose->pose.position.x;
quadPos.pose.position.y = pubQuadPose->pose.position.y;
quadPosAcquired = 1;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "quadcopterRosCtrl");
ros::NodeHandle n;
ros::Publisher targetObjPos_pub = n.advertise<geometry_msgs::PoseStamped>("vrep/targetObjPos", 100);
ros::Subscriber quadcopPos_sub = n.subscribe("vrep/quadcopPos", 100, quadPosFromVrep);
Cao::SayHello();
[ SOME CODE IN THE MIDDLE ]
return 0;
}
And I didn't manage to get rid of this error in any way. It's something related to the code or to the CMakeList.txt?? The full output error after catkin_make is the following:
CMakeFiles/quadcopterRosCtrl.dir/src/quadcopterRosCtrl.cpp.o: In function `main':
/home/francescow/catkin_ws/src/quadcopter_ctrl/src/quadcopterRosCtrl.cpp:103: undefined reference to `Cao::SayHello()'
collect2: ld returned 1 exit status
make[2]: *** [/home/francescow/catkin_ws/devel/lib/quadcopter_ctrl/quadcopterRosCtrl] Error 1
make[1]: *** [quadcopter_ctrl/CMakeFiles/quadcopterRosCtrl.dir/all] Error 2
make: *** [all] Error 2
Thanks in advance.
show full error pls
Just edited. I've made some slight changes but error is always the same.
Thanks BP, it worked!!! Although I was missing some few other things, like where to put the linking instruction, and the meaning of the syntax, but definitely that was the problem. (Can I answer clearly my own question just for the records or that's enough?)
I don't know. In future try to look up things in tutorials (for instance CMakeList - there are also descriptions).