用g++去编译下列代码报错,找不到错误的地方在哪里,用的是C++17;
person.h的代码:
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>
#include <vector>
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/dnn.hpp"
typedef class basic_Person Person;
const double cosine_similar_thresh = 0.363;
const double l2norm_similar_thresh = 1.128;
class basic_Person
{
protected:
const std::string path, name;
cv::Mat feature, face;
public:
basic_Person(basic_Person &) = delete; // 禁止拷贝
basic_Person(const std::string &PATH, const std::string &NAME);
virtual bool init(cv::Ptr<cv::FaceDetectorYN>, cv::Ptr<cv::FaceRecognizerSF>);
std::string Perosn_name() const;
std::string Perosn_path() const;
cv::Mat Person_feature() const;
cv::Mat Person_face() const;
virtual cv::Mat Person_frame();
virtual uchar status() = 0;
};
class user : public basic_Person
{
public:
user(const std::string &PATH, const std::string &NAME);
uchar status() { return 0x01; }
};
class admin : public basic_Person
{
public:
admin(const std::string &PATH, const std::string &NAME);
uchar status() { return 0x03; }
};
class stranger final : public basic_Person
{
public:
stranger(const cv::Mat &inputframe,const cv::Mat &targetfaces,int i);
virtual bool init(cv::Ptr<cv::FaceDetectorYN> detector, cv::Ptr<cv::FaceRecognizerSF> faceRecognizer) override;
virtual cv::Mat Person_frame() override;
uchar status() { return 0x00; }
private:
cv::Mat frame;
};
bool build_target(const cv::Mat &frame,cv::Ptr<cv::FaceDetectorYN> detector,std::vector<std::shared_ptr<stranger>> target);
#endif
person .cpp代码
#include "person.h"
basic_Person::basic_Person(const std::string &PATH, const std::string &NAME) : path(PATH), name(NAME) {}
user::user(const std::string &PATH, const std::string &NAME) : basic_Person(PATH, NAME) {}
admin::admin(const std::string &PATH, const std::string &NAME) : basic_Person(PATH, NAME) {}
std::string basic_Person::Perosn_name() const
{
return name;
}
std::string basic_Person::Perosn_path() const
{
return path;
}
cv::Mat basic_Person::Person_feature() const
{
return feature;
}
cv::Mat basic_Person::Person_face() const
{
return face;
}
cv::Mat basic_Person::Person_frame()
{
cv::Mat image = cv::imread(cv::samples::findFile(path));
if (image.empty())
{
std::cerr << "Cannot read image: " << path << std::endl;
}
return image;
}
// 初始化feature and face
bool basic_Person::init(cv::Ptr<cv::FaceDetectorYN> detector, cv::Ptr<cv::FaceRecognizerSF> faceRecognizer)
{
cv::Mat frame = Person_frame();
int imageWidth = int(frame.cols);
int imageHeight = int(frame.rows);
cv::resize(frame, frame, cv::Size(imageWidth, imageHeight));
detector->setInputSize(frame.size());
detector->detect(frame, face);
if (face.rows < 1)
{
std::cerr << "Cannot find a face in " << path << std::endl;
return false;
}
cv::Mat aligned_face; // 为了避免重复创建
faceRecognizer->alignCrop(this->Person_frame(), face.row(0), aligned_face);
faceRecognizer->feature(aligned_face, feature);
return true;
}
cv::Mat stranger::Person_frame() { return frame; }
stranger::stranger(const cv::Mat &inputframe, const cv::Mat &targetfaces, int i) : basic_Person("/none", "stranger"), frame(inputframe) { targetfaces.row(i).copyTo(face.row(0)); }
希望学长们能给出为什么编译不通过的原因和指出我这代码存在的错误之处;
如果只是大概猜原因我是不会采纳的,我要准确的,可以对应在primer之类的书籍上的知识点的分析。
谢谢。
缺少stranger::init的实现
virtual bool stranger::init(cv::Ptr<cv::FaceDetectorYN> detector, cv::Ptr<cv::FaceRecognizerSF> faceRecognizer) override;
不对:请仔细看一下代码!!!
1、你看一下代码,我在基类中声明的纯虚函数是=0的,然后在派生类中都有了各自的实现。而其他的虚成员函数都是要么在基类中有实现了,要么派生类中实现,并没有出现缺少的情况。
2、我在.cpp中实现了cv::Mat stranger::Person_frame() { return frame; }。
请仔细看一下代码。
在这份代码中,我没有看到明显的语法错误。如果您使用g++编译时出现错误,可能是因为您的编译器没有正确链接所需的库文件。您需要确认您是否已经安装了OpenCV库,并使用正确的编译选项和库路径来编译代码。
您可以尝试以下命令编译代码(假设您已经安装了OpenCV库):
g++ -std=c++17 -I /path/to/opencv/include -L /path/to/opencv/lib -o main main.cpp -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_objdetect -lopencv_videoio -lopencv_dnn
请注意,/path/to/opencv
应该替换为您实际的OpenCV安装路径。