为什么总是显示我重定义? - V2EX
codechaser
V2EX    C

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

  •  
  •   codechaser Apr 16, 2019 3174 views
    This topic created in 2585 days ago, the information mentioned may be changed or developed.

    大家好! 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) { reurn 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 replies    2019-04-28 12:08:04 +08:00
    choury
        1
    choury  
       Apr 16, 2019 via Android
    你这个 camera.h 是不是还有其他 cpp 文件直接或者间接 include 了?如果有那就重定义了,如果一定要写在头文件里,函数实现要写在类定义里,不能写外面
    ysc3839
        2
    ysc3839  
       Apr 16, 2019
    看看 map.cpp 和 frame.cpp ?
    另外你应该把 camera.h 最下面那两个构造函数写在 camera.cpp 里面。
    geelaw
        3
    geelaw  
       Apr 16, 2019 via iPhone
    那是因为 camera.cpp 和其他包含了多少 camera.h 的对象文件都包括了那两个构造器。

    你可以把构造器定义写到 class scope 里面,或者挪到 camera.cpp 里面,就不会有问题了。
    geelaw
        4
    geelaw  
       Apr 16, 2019 via iPhone
    @geelaw #3 去掉“多少”(智障的输入法联想,非要在空格之后自动加上去)。
    codechaser
        5
    codechaser  
    OP
       Apr 16, 2019
    @choury
    @geelaw
    @ysc3839
    各位晚上好!我好像明白了,因为我在`frame.h`引用了`camera.h`,然后`map.h`里又引用了`frame.h`导致了重定义.我也明白了为啥放在 camera.cpp 里不会报错.但是为啥放在`camera.h`类定义里面不会报错呢?我试了一下,会报错,显示构造函数 can not be overloaded
    geelaw
        6
    geelaw  
       Apr 16, 2019 via iPhone
    @codechaser #5 你可能忘记删除(多余的、不带定义的)声明了。
    crayontxx
        7
    crayontxx  
       Apr 28, 2019
    你定义在 camera.h 的类定义内,会自动变为 inline。inline 允许 multi definitions,只是会用后一个 def 覆盖前一个。
    About     Help     Advertise     Blog     API     FAQ     Solana     5809 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 71ms UTC 01:42 PVG 09:42 LAX 18:42 JFK 21:42
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86