Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C++;模板子类? 如何在C++模板子类中重写转换运算符? 例如,我尝试从openCv实现Rect_uuu模板类的子类: template<typename _Tp> class Rect_ { public: typedef _Tp value_type; //! various constructors Rect_(); Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height); Rect_(const Rect_& r); Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz); Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2); Rect_& operator = ( const Rect_& r ); //! the top-left corner Point_<_Tp> tl() const; //! the bottom-right corner Point_<_Tp> br() const; //! size (width, height) of the rectangle Size_<_Tp> size() const; //! area (width*height) of the rectangle _Tp area() const; //! conversion to another data type template<typename _Tp2> operator Rect_<_Tp2>() const; //! checks whether the rectangle contains the point bool contains(const Point_<_Tp>& pt) const; _Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle }; 模板类Rect_ { 公众: typedef_Tp value_type; //!各种构造 Rect_(); 矩形(x、y、宽度、高度); Rect_(const Rect_&r); 矩形(常数点和组织,常数大小和大小); 矩形(常数点和pt1,常数点和pt2); Rect_uu&运算符=(const Rect_uu&r); //!左上角 点utl()常数; //!右下角 点br()常数; //!矩形的大小(宽度、高度) Size_uusize()常量; //!矩形的面积(宽度*高度) _Tp面积()常数; //!转换为其他数据类型 模板运算符Rect_()const; //!检查矩形是否包含该点 布尔包含(常数点)常数; _Tp x,y,width,height;//_C++_Opencv_Type Conversion_Operator Overloading_Operators - Fatal编程技术网

如何在C++;模板子类? 如何在C++模板子类中重写转换运算符? 例如,我尝试从openCv实现Rect_uuu模板类的子类: template<typename _Tp> class Rect_ { public: typedef _Tp value_type; //! various constructors Rect_(); Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height); Rect_(const Rect_& r); Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz); Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2); Rect_& operator = ( const Rect_& r ); //! the top-left corner Point_<_Tp> tl() const; //! the bottom-right corner Point_<_Tp> br() const; //! size (width, height) of the rectangle Size_<_Tp> size() const; //! area (width*height) of the rectangle _Tp area() const; //! conversion to another data type template<typename _Tp2> operator Rect_<_Tp2>() const; //! checks whether the rectangle contains the point bool contains(const Point_<_Tp>& pt) const; _Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle }; 模板类Rect_ { 公众: typedef_Tp value_type; //!各种构造 Rect_(); 矩形(x、y、宽度、高度); Rect_(const Rect_&r); 矩形(常数点和组织,常数大小和大小); 矩形(常数点和pt1,常数点和pt2); Rect_uu&运算符=(const Rect_uu&r); //!左上角 点utl()常数; //!右下角 点br()常数; //!矩形的大小(宽度、高度) Size_uusize()常量; //!矩形的面积(宽度*高度) _Tp面积()常数; //!转换为其他数据类型 模板运算符Rect_()const; //!检查矩形是否包含该点 布尔包含(常数点)常数; _Tp x,y,width,height;//

如何在C++;模板子类? 如何在C++模板子类中重写转换运算符? 例如,我尝试从openCv实现Rect_uuu模板类的子类: template<typename _Tp> class Rect_ { public: typedef _Tp value_type; //! various constructors Rect_(); Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height); Rect_(const Rect_& r); Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz); Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2); Rect_& operator = ( const Rect_& r ); //! the top-left corner Point_<_Tp> tl() const; //! the bottom-right corner Point_<_Tp> br() const; //! size (width, height) of the rectangle Size_<_Tp> size() const; //! area (width*height) of the rectangle _Tp area() const; //! conversion to another data type template<typename _Tp2> operator Rect_<_Tp2>() const; //! checks whether the rectangle contains the point bool contains(const Point_<_Tp>& pt) const; _Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle }; 模板类Rect_ { 公众: typedef_Tp value_type; //!各种构造 Rect_(); 矩形(x、y、宽度、高度); Rect_(const Rect_&r); 矩形(常数点和组织,常数大小和大小); 矩形(常数点和pt1,常数点和pt2); Rect_uu&运算符=(const Rect_uu&r); //!左上角 点utl()常数; //!右下角 点br()常数; //!矩形的大小(宽度、高度) Size_uusize()常量; //!矩形的面积(宽度*高度) _Tp面积()常数; //!转换为其他数据类型 模板运算符Rect_()const; //!检查矩形是否包含该点 布尔包含(常数点)常数; _Tp x,y,width,height;//,c++,opencv,type-conversion,operator-overloading,operators,C++,Opencv,Type Conversion,Operator Overloading,Operators,我的子类ColorRect有额外的字段颜色。如何从父类重写转换运算符?如果不可能,我可以呼叫转换操作员吗?我怎么做 template<typename _Tp> class ColorRect_ : public cv::Rect_<_Tp> { uchar color; ColorRect_(): cv::Rect_() { color = NONE; } ColorRect_(_Tp _x, _Tp _y, _Tp _width, _Tp _

我的子类ColorRect有额外的字段颜色。如何从父类重写转换运算符?如果不可能,我可以呼叫转换操作员吗?我怎么做

template<typename _Tp> class ColorRect_ : public cv::Rect_<_Tp>
{
    uchar color;

    ColorRect_(): cv::Rect_() { color = NONE; }
    ColorRect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height, uchar color) : cv::Rect_(_x, _y, _width, _height), color(color)
    {

    }

    ColorRect_(const ColorRect_& r) : cv::Rect_(r), color(r.color) {}

    ColorRect_& operator = (const ColorRect_& r)
    {
        cv::Rect_<_Tp>::operator=(r);
        color = r.color;
        return this;
    }

    template<typename _Tp2> operator ColorRect_<_Tp2>() const
    {
        ... ?
    }
};
template类ColorRect\公共cv::Rect_
{
乌查尔色;
ColorRect_U3;():cv::Rect_U3;(){color=NONE;}
颜色矩形(x,y,宽度,高度,uchar颜色):cv::矩形(x,y,宽度,高度),颜色(颜色)
{
}
ColorRect_u3;(const ColorRect_3;&r):cv::Rect_3;(r),color(r.color){}
ColorRect\uR运算符=(const ColorRect\uR&r)
{
cv::Rect_uz::运算符=(r);
颜色=r.color;
归还这个;
}
模板运算符ColorRect_U9;()常量
{
... ?
}
};

提前感谢。

您可以创建非显式构造函数,从父实例构造类的实例:

ColorRect_(const cv::Rect_<_Tp>& other) {...};
ColorRect_(const cv::Rect_&o){…};

它将以与转换运算符相同的方式工作。

无关,但用下划线开头的标识符紧跟大写字母,在C++中保留。