What is the wrong with my C++ interfacing

Ferhat Kurtulmuş aferust at gmail.com
Sun Mar 15 19:39:31 UTC 2020


The original C++ class
https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core/types.hpp#L315:
template<typename _Tp> class Size_
{
public:
     typedef _Tp value_type;

     //! default constructor
     Size_();
     Size_(_Tp _width, _Tp _height);
     Size_(const Size_& sz);
     Size_(Size_&& sz) CV_NOEXCEPT;
     Size_(const Point_<_Tp>& pt);

     Size_& operator = (const Size_& sz);
     Size_& operator = (Size_&& sz) CV_NOEXCEPT;
     //! the area (width*height)
     _Tp area() const;
     //! aspect ratio (width/height)
     double aspectRatio() const;
     //! true if empty
     bool empty() const;

     //! conversion of another data type.
     template<typename _Tp2> operator Size_<_Tp2>() const;

     _Tp width; //!< the width
     _Tp height; //!< the height
};

// my auxiliary cpp code:
cv::Size_<int>* createSizeIntWH(int _width, int _height){
     return new cv::Size_<int>(_width, _height);
}

void deleteSizeInt(cv::Size_<int> *&sz){
     delete sz;
}

// d code:
extern(C++, cv){
class Size_(_Tp){
     @disable this();

     final _Tp area() const;
     final double aspectRatio() const;
     final bool empty() const;

     _Tp width; //!< the width
     _Tp height; //!< the height
}
}

// my test code that fails:
Size_!int sz = createSizeIntWH(200, 100);
writeln(sz.width);

One of the problems is that sz.width is not printed as 200, but a 
random int.
Other problem is that if I try to call one of area, aspectRatio, 
and empty, it does not compile yielding a linker error:
error LNK2019: unresolved external symbol "public: int __cdecl 
cv::Size_<int>::area(void)const " (?area@?$Size_ at H@cv@@QEBAHXZ) 
referenced in function _Dmain
Somehow linker cannot locate that symbol. Is this a name mangling 
issue?
In the same library I could successfully interface cv::Mat which 
is a template-free definition. I suspect if D allows interfacing 
C++ class templates since docs do not cover this, but only struct 
templates?



More information about the Digitalmars-d-learn mailing list