链接错误的未定义引用

undefined reference to linking error

本文关键字:引用 未定义 错误 链接      更新时间:2023-10-16

我的项目中出现了"undefined reference to Mapmatcher::ransacMatches(cv::Mat, cv::Mat, Pose&)"链接错误。我试着创建了一个MWE,如下所示,相信错误会很清楚。我的猜测是,我需要将Mapmatcher::放在函数前面,但正如我在class Mapmatcher{}中声明的那样,应该没有必要。

map_matcher_test_lib.h:

class Mapmatcher{
    public:
        Mapmatcher(void){};
        void ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose);
};

map_matcher_test_lib.cpp:

#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...
            }
    };
}

map_matcher_test_node.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();
}

感谢您的帮助。

您在头文件中有一次class Mapmatcher,然后在源文件中又有一次,这是一个错误,违反了一次性定义规则。您应该只在头文件中有类定义,并在源文件中实现方法:

map_matcher_test_lib.h

class Mapmatcher{
    public:
        Mapmatcher(void){};
        void ransacMatches(cv::Mat matGlob, cv::Mat matLoc, Pose &pose);
};

map_matcher_test_lib.cpp:

#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的类定义也在标头中的命名空间中。