I am using OpenCV and I do not like the ouput of:
std::cout << matrix << std::endl;
when matrix has type cv::Mat
.
Is it possible to redefine the effect of operator <<
on objects of an existing class without having to modify the code of the class ?
I know that I could write a simple function that would produce a string out of a cv::Mat
but the result would be less readable (I think) and I am a beginner in C++ so I may have missed something.
I found this SO question so I tried:
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
std::ostream& operator<<(std::ostream& os, const cv::Mat& mat)
{
os << "test";
return os;
}
int main(int argc, char** argv)
{
cv::Mat m(2,2, CV_8UC3, cv::Scalar(0,0,255));
std::cout << m << std::endl;
}
But I got:
main.cpp:14:18: error: ambiguous overload for ‘operator<<’ in ‘std::cout << m’
edit: I do not think it is a duplicate of this question because I do not have access to the code of the library (OpenCV is opensource so I could in theory modify it but that would be a bad idea: harder to maintain, to redistribute my code, etc.).