我正在文件。hpp中编写一个类,我使用VS2019和opencv 3.4.1。它返回以下错误:
Exception thrown: read access violation.
this->p was nullptr. at line 1419 of file mat.inl.hpp
当我只编译。hpp文件时,它成功了,没有错误,但如果我试图在另一个文件中声明这个类,它将返回这个错误:
错误图片
这是我的文件。hpp的样子
#pragma once
#include "Header_1.h"
class GUI_1
{
private:
//Create a black image
Mat imgBlank = Mat::zeros(original.size(), CV_8UC3);
//Take size original to 2 part
int height = original.size().height;
int width = original.size().width;
public:
//Sources img (input)
Mat original;
//extension coordinate variable for tracking object
int posX;
int posY;
//last line of input
//color for cross hair and coordinate
double red = 0;
double green = 255;
double blue = 255;
//function
Mat crosshair()
{
// Crate blank img
Mat img = imgBlank;
//properties
int length = 10;
//draw crosshair
//horizon
line(img, Point(posX - length, posY), Point(posX + length, posY), Scalar(blue, green, red), 1);
//vertical
line(img, Point(posX, posY - length), Point(posX, posY + length), Scalar(blue, green, red), 1);
return img;
}
Mat imgCoordinate()
{
//blank img
Mat img = imgBlank;
//coordinate of text with object
const int subX = 2;
const int subY =2;
//put coordinate
string Text = "X=" + to_string(posX) + "Y=" + to_string(posY);
putText(img, Text, Point(posX + subX, posY + subY), FONT_HERSHEY_COMPLEX, 1, Scalar(blue, green, red), 1);
return img;
}
//Create imgGUI for show (output)
Mat imgGUI = imgBlank + crosshair() + imgCoordinate();
};
考虑有相同问题的代码:
#include <iostream>
struct MatSize {
int value = 42;
int operator()(){ return value; }
};
struct Mat {
MatSize size;
};
struct Foo {
int x = m.size();
Mat m;
};
int main(int argc, char* argv[]){
Foo f;
std::cout << f.x;
}
可能输出为0
。虽然它可以打印任何其他东西,因为代码有未定义的行为。成员按其在类定义中列出的顺序初始化。因此,x = m.size();
读取未初始化的MatSize::value
。在调试版本中,当您在尚未初始化的实例上调用MatSize::operator()
时,可能会有一些安全网产生运行时错误。
注意初始化顺序。
struct Foo {
Mat m;
int x = m.size();
};