"winterTTr" <winter...@gmail.com>
??????:5f7a4bd4-cfd2-49e5-8ecf-2317f4701...@k39g2000yqb.googlegroups.com...
Hi , I just want to write a template function to accomplish some
kind of action.
But i meet a problem that the compiler always say that "no
matching function for call".
There must be some problem about the function declaration or
using, but i can't find it.
Please help me to point out that the problem is, thanks.
The code is like below( i remove the most of the detail part )
template < typename _Iterator , typename _Compare >
void bubbleSort(
_Iterator __first ,
_Iterator __last ,
_Compare __compare = less< typename
iterator_traits<_Iterator>::value_type >() )
{
typedef typename iterator_traits<_Iterator>::value_type
_value_type;
copy( __first , __last , ostream_iterator< _value_type > ( cout
, " " ) );
};
int main(int argc, char const* argv[])
{
vector<int> m;
m.push_back( 1 );
m.push_back( 2 );
m.push_back( 3 );
m.push_back( 4 );
bubbleSort( m.begin() , m.end() ); //
error !! Can't find the function to call
bubbleSort( m.begin() , m.end() , less<int>() ); // this is OK.
return 0;
}
Can you tell me why the error comes out when i want to use the
default value?
Or Is there a way to prevent this error as well as using the
default value for the _Compare argument?
you should implement two verion of bubbleSort function template.
one is for general version, another is for partitial.
general version:
template < typename _MyIterator , typename _MyCompare>
void bubbleSort(
_MyIterator __first ,
_MyIterator __last ,
_MyCompare __compare )
{
typedef typename iterator_traits<_MyIterator>::value_type
_value_type;
copy( __first , __last , ostream_iterator< _value_type > ( cout ,
" " ) );
cout << "general version" << endl;
};
partitial version:
template < typename _MyIterator >
void bubbleSort(
_MyIterator __first ,
_MyIterator __last ,
less< typename
iterator_traits<_MyIterator>::value_type > __compare = less<
typename
iterator_traits<_MyIterator>::value_type >() )
{
typedef typename iterator_traits<_MyIterator>::value_type
_value_type;
copy( __first , __last , ostream_iterator< _value_type > ( cout ,
" " ) );
cout << "partitial version" << endl;
};
run result:
1 2 3 4 partitial version
1 2 3 4 general version
version of "sort" without the "compare" argument ).
do it.