库的 C++ 重载赋值运算符 class
C++ overload assignment operator of a library class
我需要将 cv::Mat 分配给 cv::Point3d
cv::Point3d pt;
cv::Mat mat(3, 1, CV_64F);
pt = mat;
我尝试了两种不同的方式。第一次尝试如下:
template<typename _Tp>
inline cv::Point3_<_Tp> & cv::Point3_<_Tp>::operator = (const cv::Mat & mat){ ... }
但它提供了以下编译错误:
Out-of-line definition of 'operator=' does not match any declaration in 'Point3_<_Tp>'
我也试过这个不同的解决方案:
template<typename _Tp>
inline cv::Mat::operator cv::Point3_<_Tp>() const { }
然而,编译器不喜欢它并提供以下错误:
Out-of-line definition of 'operator Point3_<type-parameter-0-0>' does not match any declaration in 'cv::Mat'
我错过了什么?
您不能在 class 定义之外定义赋值或转换运算符。他们必须是 members of the class.
您可以做的是提供您自己的包装器 class,它允许这样的分配。类似于:
namespace mycv
{
class Point3d
{
public:
template <typename... Args>
Point3d(Args&& ... args)
: value(std::forward(args)...)
{}
Point3d& operator=(cv::Mat const& mat)
{
// do your stuff;
return *this;
}
operator cv::Point3d() const
{
return value;
}
private:
cv::Point3d value;
};
}
int main(int argc, const char* argv[])
{
mycv::Point3d pt;
cv::Mat mat;
pt = mat;
}
我需要将 cv::Mat 分配给 cv::Point3d
cv::Point3d pt;
cv::Mat mat(3, 1, CV_64F);
pt = mat;
我尝试了两种不同的方式。第一次尝试如下:
template<typename _Tp>
inline cv::Point3_<_Tp> & cv::Point3_<_Tp>::operator = (const cv::Mat & mat){ ... }
但它提供了以下编译错误:
Out-of-line definition of 'operator=' does not match any declaration in 'Point3_<_Tp>'
我也试过这个不同的解决方案:
template<typename _Tp>
inline cv::Mat::operator cv::Point3_<_Tp>() const { }
然而,编译器不喜欢它并提供以下错误:
Out-of-line definition of 'operator Point3_<type-parameter-0-0>' does not match any declaration in 'cv::Mat'
我错过了什么?
您不能在 class 定义之外定义赋值或转换运算符。他们必须是 members of the class.
您可以做的是提供您自己的包装器 class,它允许这样的分配。类似于:
namespace mycv
{
class Point3d
{
public:
template <typename... Args>
Point3d(Args&& ... args)
: value(std::forward(args)...)
{}
Point3d& operator=(cv::Mat const& mat)
{
// do your stuff;
return *this;
}
operator cv::Point3d() const
{
return value;
}
private:
cv::Point3d value;
};
}
int main(int argc, const char* argv[])
{
mycv::Point3d pt;
cv::Mat mat;
pt = mat;
}