无法编译.头文件.包含自己的对象定义

Not possible to compile. Headers files.Enclosed own objects definition

本文关键字:自己的 对象 定义 包含 文件 编译      更新时间:2023-10-16

在这里,我遇到了与上次类似的问题,但我找不到任何答案。

请注意,我认为很重要:通常我使用下一个命令在opencv中编译我的程序:

g++ -o def program.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv`

此行将创建一个可执行文件,其名称将为 def,我将能够使用。

正在一个项目中工作,随着它变得越来越大,我不得不定义一些对象,只是为了让一切更容易处理。我从文件中创建了一个对象:homogra.cpp和homogra.h,我用于它的comand是:

g++ -c  homogra.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv`

然后,我在程序中写了.cpp行 #include"homogra.h"

我编译成:

 g++ -o def program.cpp homogra.o `pkg-config --cflags opencv` `pkg-config --libs opencv` 

到目前为止,一切正常。

然后我创建第二个对象(具有与homogra相同的编译行,但这次使用segmentator.cpp和segmentator.h),我写了 #include"segmentator.h"(在program.cpp中)的行,我编译如下:

g++ -o def program.cpp .o segmentator.o `pkg-config --cflags opencv` `pkg-config --libs opencv`

现在它不起作用,并且无法识别分割器。我已经检查了分割器是否正常工作,如果 homogra 是程序中唯一的包含,一切正常.cpp。

我注意到一些奇怪的事情。如果我更改行并且之前在 #include 行中写入"segmentator.h",然后 #include #include"homogra.h",则编译器使用相同的行进行编译:

 g++ -o def program.cpp homogra.o segmentator.o `pkg-config --cflags opencv` `pkg-config --libs opencv`

只识别这个时间分割器,而不是同质。这可能有点难以理解,我试图尽可能地解释它。

有什么帮助吗!?

提前非常感谢。

这是homogra.h:

using namespace cv;
using namespace std;
#ifndef _NAMES_H  
#define _NAMES_H   
class homogra {
public:
    Mat matCalculation( Mat img, Mat img2);
    void printMatrix(Mat matrix);
};
#endif

在homogra中.cpp我有所有典型的包含和homogra.h:

#include <stdio.h>
#include <stdlib.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <sstream>
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "homogra.h"

然后解释函数。

对象 2 是分段器。

 using namespace cv;
 using namespace std;
#ifndef _NAMES_H  
#define _NAMES_H   
 class segmentator {
public:
    void search(Mat img,vector<std::vector<cv::Point> >& contours);
     void similar(vector<std::vector<cv::Point> >& contours,vector<std::vector<cv::Point> >& contours2,vector<int>& idx);
    vector<Mat> separate(Mat img,Mat img2,vector<std::vector<cv::Point> >&   contours,vector<std::vector<cv::Point> >& contours2,vector<int> idx);
};
#endif

在 segmentator 中.cpp我再次拥有所有相同的包含,除了 homogra.h,而不是这个,我有 segmentator.h。

程序.cpp image_reg.cpp:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "homogra.h"
#include "segmentator.h" 
 using namespace cv;
 using namespace std;
int main(int argc, char ** argv )
{ //Here is the code where I try to invoque two instances of homogra and segmentator.
}

如果我让 homogra.h 作为包含列表中第一个被读取image_reg.cpp那么只有 homogra.h 被识别,如果我让第一个位置分割器,那么只会创建 segmentator.h 实例并且 homogra. h 不会被识别。

谢谢

您的包含标头保护是错误的。它们应该是唯一的,使用源文件的名称,而不仅仅是_NAMES_H

所以在 homogra.h 中你应该有这个:

#ifndef HOMOGRA_H  
#define HOMOGRA_H   
...
#endif

。在 Segmentator.h 中,您应该拥有这个

#ifndef SEGMENTATOR_H  
#define SEGMENTATOR_H   
...
#endif

此外,在头文件中有一个using namespace xxx;是非常糟糕的做法。这会使标头很难与其他标头共存。

正如乔纳森·韦克利(Jonathan Wakely)指出的那样,以下划线开头的符号也不是一个好主意。