다른 것들 중에서도 상태 패턴을 적용하여 코드를 리팩토링하려고합니다. 나는, 그래서 좋은 주시기 바랍니다 자바 프로그래머의 더 해요, 그래서, 여기에 내가 공상 내 기본 상태 클래스, 아무것도 없어 한) :참조를 사용하여 상태 패턴 구현
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "FaceRegion.hpp"
class AlghorithmState {
public:
AlghorithmState(FaceRegion context);
virtual ~AlghorithmState();
virtual cv::Mat processImage(cv::Mat frame) = 0;
private:
FaceRegion CONTEXT;
};
와 자식 중 하나가 상태 :
class HaarClassifierState : public AlghorithmState {
public:
HaarClassifierState(FaceRegion context);
virtual ~HaarClassifierState();
cv::Mat processImage(cv::Mat frame);
};
, 그때 현재 상태를 유지하고 fromImage 방법/기능의 내부에 그것을 processImage를 호출 컨텍스트 클래스있다 :
#include "AlghoritmState.hpp"
using namespace cv;
class FaceRegion {
public:
FaceRegion();
virtual ~FaceRegion();
Mat fromImage(Mat& image);
void setAlghoritmState(AlghorithmState state); // line 10
private:
AlghorithmState alghoritm; //line
}
문제는,이 코드를 컴파일하려고, 나는 GE t 10에서 다음 오류가 발생했습니다.
In file included from AlghoritmState.hpp:15:0,
from FaceRegion.hpp:10,
from newmain.cpp:93:
FaceRegion.hpp:35:28: error: ‘AlghorithmState’ has not been declared
FaceRegion.hpp:39:5: error: ‘AlghorithmState’ does not name a type
내가 뭘 잘못 했습니까? 나는 CONTEXT 클래스 헤더 파일에 AlghoritmState의 불완전 클래스 선언을 추가하는 시도하지만 그것은 단지 또 다른 오류가 발생합니다 : 감사
In file included from AlghoritmState.hpp:15:0,
from FaceRegion.hpp:10,
from newmain.cpp:93:
FaceRegion.hpp:40:21: error: field ‘alghoritm’ has incomplete type
FaceRegion.hpp:36:10: error: cannot declare parameter ‘state’ to be of abstract type ‘AlghorithmState’
In file included from FaceRegion.hpp:10:0,
from newmain.cpp:93:
AlghoritmState.hpp:17:7: note: because the following virtual functions are pure within ‘AlghorithmState’:
AlghoritmState.hpp:21:21: note: virtual cv::Mat AlghorithmState::processImage(cv::Mat)
어떤 힌트를. 당신은 원형이
대단히 설명해 주셔서 감사합니다.이 문제는 저에게 두통을주었습니다 :) 메모로 돌아가서 참조를 사용하기 위해 다시 작성했지만 다른 장애물에 붙어 있습니다. 이렇게되면 다음과 같이됩니다. HaarClassifierState에 .hpp : 'public : HaarClassifierState (FaceRegion & context); virtual ~ HaarClassifierState(); cv :: Mat processImage (cv :: Mat frame); 개인 : FaceRegion 및 CONTEXT; HaarClassifierState.cpp에서' : 'HaarClassifierState :: HaarClassifierState (FaceRegion 및 컨텍스트) : CONTEXT (문맥) {}' –
그리고, FaceRegion.hpp에서 : '클래스 FaceRegion { 공개 : void setAlghoritmState (AlghorithmState & state); 개인 : AlghorithmState & algorithm; }' 초기화 목록에서 변수를 초기화 할 수 없기 때문에 알고리즘 필드를 처음부터 초기화 할 수 없습니다. (또는 할 수 있습니까?) 'FaceRegion :: FaceRegion() : algorithm (HaarClassifierState (* this)) {}' : 오류 : 'HaarClassifierState'유형의 '값'에서 'AlghorithmState &'유형의 비 const 참조를 초기화하지 못했습니다. –