Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   count_if bind2nd greater<string> = error??? (http://www.velocityreviews.com/forums/t743934-count_if-bind2nd-greater-string-error.html)

cpp 02-21-2011 04:32 PM

count_if bind2nd greater<string> = error???
 
In the code below I'm trying to cound the number of times a string
reater than B occours with count_if and bind2nd.

The example on ints in vector vi works fine.

But the code for the string case does not compile. I'm *sure* this
should work, but I just cannot see what is wrong.

Could someone point me in the right direction pls - I'm tearing my
hair out.

Compiler is Visual Studio 2005 SP2

-----------------------------------------
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;


int main()
{
std::vector<std::string> s;
vector<int> vi;
// fill vectors

std::cout << std::count_if(
vi.begin(),
vi.end(),
std::bind2nd(std::greater<int>(),1));

std::cout << std::count_if(
s.begin(),
s.end(),
std::bind2nd(std::greater<string>(),"B")); //
compile error here ???

return 0;
}

end the error is :

1>------ Build started: Project: TopCoder_Arena, Configuration: Debug
Win32 ------
1>Compiling...
1>TopCoder.cpp
1>g:\program files\microsoft visual studio 8\vc\include
\functional(132) : error C2784: 'bool std::operator >(const
std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could
not deduce template argument for 'const std::vector<_Ty,_Alloc> &'
from 'const std::string'
1> g:\program files\microsoft visual studio 8\vc\include
\vector(1285) : see declaration of 'std::operator >'
1> g:\program files\microsoft visual studio 8\vc\include
\functional(131) : while compiling class template member function
'bool std::greater<_Ty>::operator ()(const _Ty &,const _Ty &) const'
1> with
1> [
1> _Ty=std::string
1> ]
1> g:\code\c++\visual studio projects\topcoder_arena
\topcoder.cpp(20) : see reference to class template instantiation
'std::greater<_Ty>' being compiled
1> with
1> [
1> _Ty=std::string
1>

Victor Bazarov 02-21-2011 04:40 PM

Re: count_if bind2nd greater<string> = error???
 
On 2/21/2011 11:32 AM, cpp wrote:
> In the code below I'm trying to cound the number of times a string
> reater than B occours with count_if and bind2nd.
>
> The example on ints in vector vi works fine.
>
> But the code for the string case does not compile. I'm *sure* this
> should work, but I just cannot see what is wrong.
>
> Could someone point me in the right direction pls - I'm tearing my
> hair out.
>
> Compiler is Visual Studio 2005 SP2
>
> -----------------------------------------
> #include<iostream>
> #include<vector>
> #include<functional>
> #include<algorithm>
> using namespace std;
>
>
> int main()
> {
> std::vector<std::string> s;
> vector<int> vi;
> // fill vectors
>
> std::cout<< std::count_if(
> vi.begin(),
> vi.end(),
> std::bind2nd(std::greater<int>(),1));
>
> std::cout<< std::count_if(
> s.begin(),
> s.end(),
> std::bind2nd(std::greater<string>(),"B")); //
> compile error here ???


"B" is not a string. Perhaps you could construct a temporary to pass it
to 'bind2nd':

... bind2nd(greater<string>(), string("B"));

Just a shot in the dark, really.

>
> return 0;
> }
>
> end the error is :
>
> 1>------ Build started: Project: TopCoder_Arena, Configuration: Debug
> Win32 ------
> 1>Compiling...
> 1>TopCoder.cpp
> 1>g:\program files\microsoft visual studio 8\vc\include
> \functional(132) : error C2784: 'bool std::operator>(const
> std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could
> not deduce template argument for 'const std::vector<_Ty,_Alloc> &'
> from 'const std::string'
> 1> g:\program files\microsoft visual studio 8\vc\include
> \vector(1285) : see declaration of 'std::operator>'
> 1> g:\program files\microsoft visual studio 8\vc\include
> \functional(131) : while compiling class template member function
> 'bool std::greater<_Ty>::operator ()(const _Ty&,const _Ty&) const'
> 1> with
> 1> [
> 1> _Ty=std::string
> 1> ]
> 1> g:\code\c++\visual studio projects\topcoder_arena
> \topcoder.cpp(20) : see reference to class template instantiation
> 'std::greater<_Ty>' being compiled
> 1> with
> 1> [
> 1> _Ty=std::string
> 1>


V
--
I do not respond to top-posted replies, please don't ask

cpp 02-21-2011 04:43 PM

Re: count_if bind2nd greater<string> = error???
 
> "B" is not a string. Perhaps you could construct a temporary to pass it


Thanks,
yes I tought that it would be converted implicity. But even when I try
and explicit conversion as you suggest it still fails.
:-/.


Different error though:

1>g:\program files\microsoft visual studio 8\vc\include
\functional(132) : error C2784: 'bool std::operator >(const
std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could
not deduce template argument for 'const std::vector<_Ty,_Alloc> &'
from 'const std::string'
1> g:\program files\microsoft visual studio 8\vc\include
\vector(1285) : see declaration of 'std::operator >'
1> g:\program files\microsoft visual studio 8\vc\include
\functional(131) : while compiling class template member function
'bool std::greater<_Ty>::operator ()(const _Ty &,const _Ty &) const'
1> with
1> [
1> _Ty=std::string
1> ]
1> g:\duffyj\c++\visual studio projects\topcoder_arena
\topcoder.cpp(23) : see reference to class template instantiation
'std::greater<_Ty>' being compiled
1> with
1> [
1> _Ty=std::string
> "B" is not a string. *Perhaps you could construct a temporary to pass it
> to 'bind2nd':
>
> * * ... bind2nd(greater<string>(), string("B"));
>


1>g:\program files\microsoft visual studio 8\vc\include
\functional(132) : error C2784: 'bool std::operator >(const
std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could
not deduce template argument for 'const std::vector<_Ty,_Alloc> &'
from 'const std::string'
1> g:\program files\microsoft visual studio 8\vc\include
\vector(1285) : see declaration of 'std::operator >'
1> g:\program files\microsoft visual studio 8\vc\include
\functional(131) : while compiling class template member function
'bool std::greater<_Ty>::operator ()(const _Ty &,const _Ty &) const'
1> with
1> [
1> _Ty=std::string
1> ]
1> g:\duffyj\c++\visual studio projects\topcoder_arena
\topcoder.cpp(23) : see reference to class template instantiation
'std::greater<_Ty>' being compiled
1> with
1> [
1> _Ty=std::string

cpp 02-21-2011 04:46 PM

Re: count_if bind2nd greater<string> = error???
 
> Problem is probably due to you not doing:
>
> #include <string>
>
> /Leigh- Hide quoted text -
>



OMG!!! I hang my head in shame. I spent all day on that :-(.

Interesting it complies on Comeau without the sting header being
included.

Thanks!!!

SL99

Stuart Redmann 02-22-2011 03:00 PM

Re: count_if bind2nd greater<string> = error???
 
On 21 Feb., cpp wrote:
> Interesting it complies on Comeau without the sting header being
> included.


Note that you must not rely on implicit includes in standard headers,
as this will become the source of hard to track bugs. Even worse,
there are cases where perfectly legal C++ code will (legally) not
compile with one compiler and (also legally) not compile with another
due to the fact that one has a declaration in namespace std that is
missing in the other.

Regards,
Stuart


All times are GMT. The time now is 12:20 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57