V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
codechaser
V2EX  ›  C

为什么总是显示我重定义?

  •  
  •   codechaser · 2019-04-16 00:38:05 +08:00 · 2380 次点击
    这是一个创建于 1808 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大家好! camera.h:

    #ifndef CAMERA_H
    #define CAMERA_H
    
    #include "myslam/common_include.h"
    #include <myslam/config.h>
    
    /**
     *小孔成像相机模型
     */
     
    namespace myslam
    {
    class Camera
    {
    public:
        typedef std::shared_ptr<Camera> Ptr;
        //定义相机参数及尺度
        float fx, fy, cx, cy, depth_scale;
        Camera();
        Camera(float fx_, float fy_, float cx_, float cy_, float depth_scale_ = 0);
    
        Vector3d world2camera(const Vector3d &p_w, const SE3 &Tcw);
        Vector3d camera2world(const Vector3d &p_c, const SE3 &Tcw);
        Vector2d camera2pixel(const Vector3d &p_c);
        Vector3d pixel2camera(const Vector2d &p_p, double depth = 1);
        Vector3d pixel2world(const Vector2d &p_p, const SE3 &Tcw, double depth = 1);
        Vector2d world2pixel(const Vector3d &p_w, const SE3 &Tcw);
    };
    Camera::Camera(float fx_, float fy_, float cx_, float cy_, float depth_scale_)
        : fx(fx_), fy(fy_), cx(cx_), cy(cy_), depth_scale(depth_scale_) {}
    
    Camera::Camera()
    {
        fx = Config::get<float>("camera.fx");
        fy = Config::get<float>("camera.fy");
        cx = Config::get<float>("camera.cx");
        cy = Config::get<float>("camera.cy");
        depth_scale = Config::get<float>("camera.depth_scale");
    }
    } // namespace myslam
    
    #endif // !CAMERA_H
    

    camera.cpp:

    #include <myslam/camera.h>
    
    namespace myslam
    {
    Vector3d Camera::world2camera(const Vector3d &p_w, const SE3 &Tcw)
    {
        //Tcw 是李群
        return Tcw * p_w;
    }
    Vector3d Camera::camera2world(const Vector3d &p_c, const SE3 &Tcw)
    {
        return Tcw.inverse() * p_c;
    }
    Vector2d Camera::camera2pixel(const Vector3d &p_c)
    {
        return Vector2d(fx * p_c(0) / p_c(2) + cx, fy * p_c(1) / p_c(2) + cy);
    }
    Vector3d Camera::pixel2camera(const Vector2d &p_p, double depth)
    {
        return Vector3d((p_p(0) - cx) * depth / fx, (p_p(1) - cy) * depth / fy, depth);
    }
    Vector3d Camera::pixel2world(const Vector2d &p_p, const SE3 &Tcw, double depth)
    {
        return camera2world(pixel2camera(p_p, depth), Tcw);
    }
    Vector2d Camera::world2pixel(const Vector3d &p_w, const SE3 &Tcw)
    {
        return camera2pixel(world2camera(p_w, Tcw));
    }
    
    } // namespace myslam
    
    [build] CMakeFiles/myslam.dir/map.cpp.o: In function `myslam::Camera::Camera(float, float, float, float, float)':
    [build] /home/bob/CLionProjects/slam14/project/0.1/include/myslam/camera.h:29: multiple definition of `myslam::Camera::Camera(float, float, float, float, float)'
    [build] CMakeFiles/myslam.dir/frame.cpp.o:/home/bob/CLionProjects/slam14/project/0.1/include/myslam/camera.h:29: first defined here
    [build] CMakeFiles/myslam.dir/map.cpp.o: In function `myslam::Camera::Camera(float, float, float, float, float)':
    

    只要我把这两个 Camera 的构造函数放到 camera.cpp 里定义就不会报错. 现在百思不得其解,求教.

    7 条回复    2019-04-28 12:08:04 +08:00
    choury
        1
    choury  
       2019-04-16 00:45:25 +08:00 via Android
    你这个 camera.h 是不是还有其他 cpp 文件直接或者间接 include 了?如果有那就重定义了,如果一定要写在头文件里,函数实现要写在类定义里,不能写外面
    ysc3839
        2
    ysc3839  
       2019-04-16 00:46:06 +08:00
    看看 map.cpp 和 frame.cpp ?
    另外你应该把 camera.h 最下面那两个构造函数写在 camera.cpp 里面。
    geelaw
        3
    geelaw  
       2019-04-16 00:49:11 +08:00 via iPhone
    那是因为 camera.cpp 和其他包含了多少 camera.h 的对象文件都包括了那两个构造器。

    你可以把构造器定义写到 class scope 里面,或者挪到 camera.cpp 里面,就不会有问题了。
    geelaw
        4
    geelaw  
       2019-04-16 00:49:50 +08:00 via iPhone
    @geelaw #3 去掉“多少”(智障的输入法联想,非要在空格之后自动加上去)。
    codechaser
        5
    codechaser  
    OP
       2019-04-16 01:08:02 +08:00
    @choury
    @geelaw
    @ysc3839
    各位晚上好!我好像明白了,因为我在`frame.h`引用了`camera.h`,然后`map.h`里又引用了`frame.h`导致了重定义.我也明白了为啥放在 camera.cpp 里不会报错.但是为啥放在`camera.h`类定义里面不会报错呢?我试了一下,会报错,显示构造函数 can not be overloaded
    geelaw
        6
    geelaw  
       2019-04-16 01:39:37 +08:00 via iPhone
    @codechaser #5 你可能忘记删除(多余的、不带定义的)声明了。
    crayontxx
        7
    crayontxx  
       2019-04-28 12:08:04 +08:00
    你定义在 camera.h 的类定义内,会自动变为 inline。inline 允许 multi definitions,只是会用后一个 def 覆盖前一个。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3136 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 12:37 · PVG 20:37 · LAX 05:37 · JFK 08:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.