I'm trying to write a generic function to serialize to string a std::vector<cv::Point_<T>>
and I want this to work for both cv::Point2i
and cv::Point2f
(both are typedefs of cv::Point_<T>
with a specific T).
The function looks like this:
template<typename T>
int SVIniFile::write(const std::string& section,
const std::string& key,
std::vector<cv::Point_<T>>& points)
{
std::ostringstream os;
if (points.empty())
{
return SUCCESS;
}
for (size_t j = 0; j < points.size(); j++)
{
os << points[j].x << " " << points[j].y;
if (j < points.size() - 1)
{
os << " ";
}
}
write(section, key, os.str()); // do the writing of os.str() in the right `section` at `key`
return SUCCESS; // function that writes a string into an ini file
}
Trying to compile this throws an "Unrecognizable template declaration/definition" error. Investigating on the error (full compiler output is below), I found this question, which however doesn't seem related to my case, and this question, the answer of which I don't understand.
I'm very new to template programming and I suspect the error is caused by the fact that I'm using as template parameter the type that is itself a template parameter of one of the parameters. Could anyone point me in the right direction and possibly expand a bit on why the compiler can't build this?
This is what the compiler produces as error output in every file where my header is included:
1>svinifile.h(640): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>svinifile.h(640): error C2988: unrecognizable template declaration/definition 1>svinifile.h(640): error C2143: syntax error: missing ',' before '&'
line 640 is the one right after the template<typename T>
in the function definition I show above.
Just to be explicit, cv::Point_<T>
i OpenCV's 2D point type defined in types.hpp
as:
namespace cv
{
// ...
template<typename _Tp> class Point_
{
public:
typedef _Tp value_type;
//! default constructor
Point_();
Point_(_Tp _x, _Tp _y);
Point_(const Point_& pt);
Point_(Point_&& pt) CV_NOEXCEPT;
Point_(const Size_<_Tp>& sz);
Point_(const Vec<_Tp, 2>& v);
Point_& operator = (const Point_& pt);
Point_& operator = (Point_&& pt) CV_NOEXCEPT;
//! conversion to another data type
template<typename _Tp2> operator Point_<_Tp2>() const;
//! conversion to the old-style C structures
operator Vec<_Tp, 2>() const;
//! dot product
_Tp dot(const Point_& pt) const;
//! dot product computed in double-precision arithmetics
double ddot(const Point_& pt) const;
//! cross-product
double cross(const Point_& pt) const;
//! checks whether the point is inside the specified rectangle
bool inside(const Rect_<_Tp>& r) const;
_Tp x; //!< x coordinate of the point
_Tp y; //!< y coordinate of the point
};
typedef Point_<int> Point2i;
typedef Point_<int64> Point2l;
typedef Point_<float> Point2f;
typedef Point_<double> Point2d;
typedef Point2i Point;
}