无法编译.头文件.包含自己的对象定义
Not possible to compile. Headers files.Enclosed own objects definition
在这里,我遇到了与上次类似的问题,但我找不到任何答案。
请注意,我认为很重要:通常我使用下一个命令在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)指出的那样,以下划线开头的符号也不是一个好主意。
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- C++从对象自己的类中删除对象
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- C++自己的对象堆栈.在 push() 上复制了什么?
- 鼠标单击 OpenGL 时无法围绕其自己的中心旋转对象
- C++ 在其自己的类中创建对象的修改副本
- 如何为C++映射创建自己的字符串比较对象
- 添加 swig pythoncode 以在 Python 对象上设置自己的标志
- 我是否可以像访问自己的对象一样访问对象的'base part'?
- 强制派生的类对象使用自己的功能,而不是基类功能
- 具有自己的链表的对象
- 将一个常量cstring附加到我自己的String类对象
- 从 MapViewOfFile() 的返回值构造自己的对象
- 无法编译.头文件.包含自己的对象定义
- 将我自己的对象添加到 QComboBox,Q_DECLARE_METATYPE,不完整的类型'QStaticAssertFailure<false>'
- cpp 对象方法是否有自己的堆栈帧
- 为什么我们需要维护自己的矩阵来转换游戏对象
- C++:一个类可以有它自己的类型的对象吗?
- 如何将 json c++ 与我自己的对象一起使用