不可能编译。头文件。封闭自己的对象定义 [英] Not possible to compile. Headers files.Enclosed own objects definition

查看:137
本文介绍了不可能编译。头文件。封闭自己的对象定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我有一个类似的问题,最后一次,我找不到任何答案。



注意,我认为重要的:通常我编译我的程序在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` 

然后,我在我的program.cpp中写了行#includehomogra.h



我编译为:

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


然后我创建了一个第二个对象相同的编译行为同源,但这次与segmentator.cpp和segmentator.h),我写了行#includesegmentator.h,(在program.cpp)和我编译像:

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

现在它不工作,它不能识别分段器。我检查已经如果分割器是工作,一切正常如果homogra是唯一的包含在program.cpp。



我注意到一些奇怪的东西。如果我改变行,我写之前,在#include行,#includesegmentator.h,然后#includehomogra.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>
#includeopencv2 / features2d / features2d.hpp
#includeopencv2 / calib3d / calib3d.hpp
#includehomogra.h



然后解释函数。



对象2是segmentator.h

  using namespace cv; 
using namespace std;

#ifndef _NAMES_H
#define _NAMES_H
类分割器{
public:

void search(Mat img,vector< std :: vector< cv :: Point>>& contour);

void similar(vector< std :: vector< cv :: Point>>& contours,vector< std :: vector< cv :: Point>>& contours2,vector< int& ;& idx);

vector< Mat>分离(Mat img,Mat img2,vector< std :: vector< cv :: Point>& contours,vector< std :: vector< cv :: Point>>& contours2,vector< int& ;
};

#endif

而在segmentator.cpp中我又一次除了homogra.h,而不是这个我有segmentator.h。



Program.cpp是image_reg.cpp:

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

#includehomogra.h
#includesegmentator.h
using namespace cv;
using namespace std;

int main(int argc,char ** argv)
{//这里是我试图调用两个实例的同源和分割器的代码。
}

如果我让homogra.h作为第一个在包含列表中读取of image_reg.cpp然后只有homogra.h被识别,如果我在第一个位置分割器,那么只有segmentator.h实例将被创建和同源。



感谢

解决方案

警卫是错误的。它们应该是唯一的,使用源文件的名称,而不是 _NAMES_H



所以在homogra.h中应该有:

  ifndef HOMOGRA_H 
#define HOMOGRA_H
...
#endif

... and in segmentator.h,you should have this

  #ifndef SEGMENTATOR_H 
#define SEGMENTATOR_H
...
#endif

此外,非常不好的做法在一个中使用命名空间xxx; 头文件。你使标题很难与其他人共存。



正如Jonathan Wakely指出的,带有下划线的开始符号不是一个好主意。


Here I am with a similar question as the last time, and for which I could not find any answer.

Note that I consider important: Normally I compile my programs in opencv with the next command:

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

This line will create an executable whose name will be def and that I will be able to use.

I am working in a project, and as it was getting bigger, I had to define some objects, just to make everything easier and possible to handle. I create one object from the files: homogra.cpp and homogra.h the comand I used for it was:

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

Then, I wrote in my program.cpp the line #include "homogra.h"

And I compile like:

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

Until now everything is working fine.

Then I create a second object(with the same compilation line as for homogra, but this time with segmentator.cpp and segmentator.h), i wrote the line #include "segmentator.h",(in program.cpp) and I compile like:

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

Now it is not working, and it is not recognising segmentator. I checked already if segmentator was working and everything works fine if homogra is the only include in the program.cpp.

I notice something strange. If I change the lines and I write before,in the #include lines, #include "segmentator.h" and then #include "homogra.h", then the compiler, with the same line for compiling :

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

is only recognising this time segmentator and not homogra. It is maybe a little difficult to understand, I tried to explained it as better as possible.

Any help!?

Many thanks in advance.

Here is 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

In homogra.cpp I have all the tipical includes and 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"

And then the functions explained.

Object 2 is segmentator.h

 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

And in segmentator.cpp I have again all the same includes, except homogra.h and instead of this one I have segmentator.h.

Program.cpp is 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.
}

If I let homogra.h as the first to be read in the includes list of image_reg.cpp then only homogra.h is recognised, if I let at the first position segmentator, then only segmentator.h instances would be created and homogra. h would not be recognised.

Thanks

解决方案

Your include header guards are wrong. They should be unique, using the name of the source file, rather than just _NAMES_H.

So in homogra.h you should have this:

#ifndef HOMOGRA_H  
#define HOMOGRA_H   
...
#endif

...and in segmentator.h, you should have this

#ifndef SEGMENTATOR_H  
#define SEGMENTATOR_H   
...
#endif

Also, it's really bad practice to have a using namespace xxx; in a header file. You make it very difficult for your headers to coexist with others.

As Jonathan Wakely points out, beginning symbols with underscores is not a great idea, either.

这篇关于不可能编译。头文件。封闭自己的对象定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆