C++ 链接错误的未定义引用
我的项目中出现了“C++ 链接错误的未定义引用,c++,class,pointers,constructor,ros,C++,Class,Pointers,Constructor,Ros,我的项目中出现了“对Mapmatcher::ransacMatches(cv::Mat,cv::Mat,Pose&)”链接错误的未定义引用。我尝试创建一个如下所示的MWE,并相信从中可以清楚地看到错误。我的猜测是,我需要将Mapmatcher::放在函数前面,但正如我在classmapmatcher{}中声明的那样,这应该是不必要的 map\u matcher\u test\u lib.h: class Mapmatcher{ public: Mapmatcher(voi
对Mapmatcher::ransacMatches(cv::Mat,cv::Mat,Pose&)”链接错误的未定义引用。我尝试创建一个如下所示的MWE,并相信从中可以清楚地看到错误。我的猜测是,我需要将Mapmatcher::
放在函数前面,但正如我在classmapmatcher{}
中声明的那样,这应该是不必要的
map\u matcher\u test\u lib.h:
class Mapmatcher{
public:
Mapmatcher(void){};
void ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose);
};
#include "map_matcher_test/map_matcher_test_lib.h"
namespace map_matcher_test
{
//classes
class Mapmatcher{
void ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose)
{
// some code here...
}
};
}
class Mapmatcher{
public:
Mapmatcher(void){};
void ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose);
};
#include "map_matcher_test/map_matcher_test_lib.h"
namespace map_matcher_test
{
void Mapmatcher::ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose)
{
// some code here...
}
}
map\u matcher\u test\u lib.cpp:
class Mapmatcher{
public:
Mapmatcher(void){};
void ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose);
};
#include "map_matcher_test/map_matcher_test_lib.h"
namespace map_matcher_test
{
//classes
class Mapmatcher{
void ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose)
{
// some code here...
}
};
}
class Mapmatcher{
public:
Mapmatcher(void){};
void ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose);
};
#include "map_matcher_test/map_matcher_test_lib.h"
namespace map_matcher_test
{
void Mapmatcher::ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose)
{
// some code here...
}
}
映射匹配器测试节点。cpp:
#include "map_matcher_test/map_matcher_test_lib.h"
Mapmatcher *mama = new Mapmatcher();
void mapMatcher()
{
// matGlob, matLoc, result known here
mama->ransacMatches(matGlob, matLoc, result);
}
int main (int argc, char** argv)
{
// some stuff...
mapMatcher();
}
非常感谢您的帮助。您的头文件中有一次类Mapmatcher
,然后在源文件中又有一次,这是一个错误,违反了once定义规则。您应该只在头文件中有类定义,并在源文件中实现方法:
映射匹配器测试库h:
#include "map_matcher_test/map_matcher_test_lib.h"
Mapmatcher *mama = new Mapmatcher();
void mapMatcher()
{
// matGlob, matLoc, result known here
mama->ransacMatches(matGlob, matLoc, result);
}
int main (int argc, char** argv)
{
// some stuff...
mapMatcher();
}
map\u matcher\u test\u lib.cpp:
class Mapmatcher{
public:
Mapmatcher(void){};
void ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose);
};
#include "map_matcher_test/map_matcher_test_lib.h"
namespace map_matcher_test
{
//classes
class Mapmatcher{
void ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose)
{
// some code here...
}
};
}
class Mapmatcher{
public:
Mapmatcher(void){};
void ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose);
};
#include "map_matcher_test/map_matcher_test_lib.h"
namespace map_matcher_test
{
void Mapmatcher::ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose)
{
// some code here...
}
}
请确保Mapmatcher
的类定义也在头文件的命名空间中。头文件中有一次class Mapmatcher
,源文件中又有一次,这是一个错误,违反了一次定义规则。您应该只在头文件中有类定义,并在源文件中实现方法:
映射匹配器测试库h:
#include "map_matcher_test/map_matcher_test_lib.h"
Mapmatcher *mama = new Mapmatcher();
void mapMatcher()
{
// matGlob, matLoc, result known here
mama->ransacMatches(matGlob, matLoc, result);
}
int main (int argc, char** argv)
{
// some stuff...
mapMatcher();
}
map\u matcher\u test\u lib.cpp:
class Mapmatcher{
public:
Mapmatcher(void){};
void ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose);
};
#include "map_matcher_test/map_matcher_test_lib.h"
namespace map_matcher_test
{
//classes
class Mapmatcher{
void ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose)
{
// some code here...
}
};
}
class Mapmatcher{
public:
Mapmatcher(void){};
void ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose);
};
#include "map_matcher_test/map_matcher_test_lib.h"
namespace map_matcher_test
{
void Mapmatcher::ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose)
{
// some code here...
}
}
但一定要确保Mapmatcher
的类定义也在头的名称空间中