简体   繁体   中英

C++ overload assignment operator of a library class

I need to assign a cv::Mat to a cv::Point3d

cv::Point3d pt;

cv::Mat mat(3, 1, CV_64F);

pt = mat;

I tried to do it in two different ways. The first attempt is the following:

template<typename _Tp>
inline cv::Point3_<_Tp> & cv::Point3_<_Tp>::operator = (const cv::Mat & mat){ ... }

but it provides the following compile error:

Out-of-line definition of 'operator=' does not match any declaration in 'Point3_<_Tp>'

I also tried this different solution:

 template<typename _Tp>
 inline cv::Mat::operator cv::Point3_<_Tp>() const { }

however, the compiler doesn't like it and provides the following error:

Out-of-line definition of 'operator Point3_<type-parameter-0-0>' does not match any declaration in 'cv::Mat'

What I missing?

You can not define the assignment or conversion operator outside of a class definition. They have to be members of the class .

What you could do, is to provide your own wrapper class which allows such an assignment. Something like:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM