1

在使用 PImpl Idiom 和 Curiously Recurring Template Pattern 为派生的图像类创建构造函数时,我遇到了一些编译错误。图像类实现的基类使用 CRTP(Curiously Recurring Template Pattern)来强制执行静态调度,而不是在另一个虚拟调用上分层。请告诉我我做错了什么。请参阅评论以了解错误发生的位置

// 错误:没有匹配的构造函数用于初始化 'jpeg_image::impl' [clang-diagnostic-error]

// 错误:字段的类型不完整 'jpeg_image::impl' [clang-diagnostic-error]

图像.hpp

#include <memory>
#include <vector>

namespace image_wrapper {
 
    class image {
    public:
       //ToDo: declare pure virtual functions
    };

    class jpeg_image final : public image {
        class impl;
        std::unique_ptr<impl> _impl;

    public:
        // Destructor and constructor for reading from a buffer of encoded_bytes
        ~jpeg_image();
        jpeg_image(std::vector<uint8_t> encoded_bytes);
    };
} //namespace image_wrapper 

图像.cpp

#include "image.hpp"
#include <boost/gil.hpp>
#include <functional>

namespace image_wrapper {
    namespace {

        template <class Derived>
        class impl_base {
        public:
            // ToDo: implement base class functions
        protected:
            // Constructor
            explicit impl_base(std::vector<uint8_t> encoded_bytes) :
                _unencoded_image{ std::bind(&impl_base<Derived>::initialize_unencoded_image, this) }, _encoded_bytes{ encoded_bytes }
            {
            }

        private:
            boost::gil::rgb8_image_t initialize_unencoded_image() { return static_cast<Derived&>(*this).decode_bytes(); }
            boost::gil::rgb8_image_t _unencoded_image;
            std::vector<uint8_t> _encoded_bytes;
        };
    } // namespace

    /*
     * JPEG Class Definitions
     */
    class jpeg_image::impl : public impl_base<jpeg_image::impl> {
    public:

        // Error: field has incomplete type 'jpeg_image::impl'
        jpeg_image::impl(std::vector<uint8_t> encoded_bytes) : impl_base<jpeg_image::impl>(encoded_bytes) {}

        boost::gil::rgb8_image_t decode_bytes()
        {
         // ToDo: implement decode_bytes()
        }
    };

    // Error: no matching constructor for initialization of 'jpeg_image::impl' [clang-diagnostic-error]
    jpeg_image::jpeg_image(std::vector<uint8_t> encoded_bytes) : _impl(new jpeg_image::impl(encoded_bytes)) {}

    // Jpeg Image Destructor
    jpeg_image::~jpeg_image() = default;

} // namespace image_wrapper
4

0 回答 0