2017-05-09 35 views
0

超载赋值运算符,我需要一份简历::垫分配给CV ::三维点C++库类

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' 

我缺少什么?

+0

为什么不只是制作一个免费的函数'cv :: Point3d create_point(cv :: Mat const&mat)'?然后你可以说'cv :: Point3d pt = create_point(mat);' – CoryKramer

+0

因为我更喜欢避免将显式调用函数作为create_point() – thewoz

回答

0

您不能在类定义之外定义赋值或转换运算符。他们必须是members of the 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; 
} 
+0

太糟糕了,但是谢谢你的回答。无论如何,为什么这是不可能的? – thewoz