My aim was to generalize the __gcd()
available in std <algorithm>
header in C++. which should be able to call for a range of values of a std::vector
array.
The generalized template __gcd()
works perfectly when parameters have been passed directly. Just like below:
math::GCD(1,2,58,54);
However, when I tried to pass a range of values of a vector(the code is given below), it shows the following error:
D:\Programming\C++\CPP Programs\My Test\My Test.cpp|33|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and '__gnu_cxx::__normal_iterator<int*, std::vector<int> >')|
I don't know how to overload operator<<
or include an overload function in this template/ program.(assuming that the error is due to overloading of operator<<
has missed)
namespace math
{
template <typename M, typename N>
constexpr auto GCD(const M& m, const N& n) {
return __gcd(m, n);
}
template <typename M, typename ...Rest>
constexpr auto GCD(const M& first, const Rest&... rest) {
return __gcd(first, GCD(rest...));
}
}
int main()
{
vector<int> vec={1,2,58,54,102,2,37,13,8};
for(auto i=0; i<vec.size()-3; ++i)
cout<<math::GCD(vec.begin()+i, vec.begin()+i+4)<<endl;
return 0;
}
can anybody help me to overload operator<<
or (if it's not the case) to find the error? thanks in advance.
Aucun commentaire:
Enregistrer un commentaire