944,147 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2350
  • C++ RSS
Jan 14th, 2007
0

Short code - it works on VC++ Express but not on Bloodshed!!

Expand Post »
c++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string foo = "123";
  10.  
  11. if(count_if(foo.begin(), foo.end(), isdigit) == foo.size())
  12. {
  13. cout << "\nGreat!\n";
  14. }
  15.  
  16. cin.get()
  17. }

Does not work on bloodshed v4.9.9.2!!!
It outputs the following error:
Quote ...
no matching function for call to `count_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)'
But it does work on VC++ Express 2005... (obviously including stdafx.h)
Last edited by fesago90; Jan 14th, 2007 at 9:12 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
fesago90 is offline Offline
21 posts
since Jan 2007
Jan 14th, 2007
0

Re: Short code - it works on VC++ Express but not on Bloodshed!!

I'm surprised that it even compiled on VC++ with a mistake like this:
C++ Syntax (Toggle Plain Text)
  1. cin.get()
  2. }
You also might want to read this:
http://www.sgi.com/tech/stl/count_if.html

Quote ...
The new count interface uses the iterator_traits class, which relies on a C++ feature known as partial specialization. Many of today's compilers don't implement the complete standard; in particular, many compilers do not support partial specialization. If your compiler does not support partial specialization, then you will not be able to use the newer version of count, or any other STL components that involve iterator_traits.
I'm using g++, and it supports neither count() nor count_if().
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jan 14th, 2007
0

Re: Short code - it works on VC++ Express but not on Bloodshed!!

Click to Expand / Collapse  Quote originally posted by fesago90 ...
c++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string foo = "123";
  10.  
  11. if(count_if(foo.begin(), foo.end(), isdigit) == foo.size())
  12. {
  13. cout << "\nGreat!\n";
  14. }
  15.  
  16. cin.get()
  17. }

Does not work on bloodshed v4.9.9.2!!!
It outputs the following error:


But it does work on VC++ Express 2005... (obviously including stdafx.h)
If you don't mind satisfying my curiosity, does this work in Bloodshed?
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. struct predicate {
  8. int operator()( int x ) { return isdigit( x ); }
  9. };
  10.  
  11. int main()
  12. {
  13. string foo = "123";
  14.  
  15. if(count_if(foo.begin(), foo.end(), predicate()) == foo.size())
  16. {
  17. cout << "\nGreat!\n";
  18. }
  19.  
  20. cin.get();
  21. }
Last edited by Ravalon; Jan 14th, 2007 at 9:35 pm.
Reputation Points: 84
Solved Threads: 15
Posting Whiz in Training
Ravalon is offline Offline
209 posts
since Dec 2006
Jan 14th, 2007
0

Re: Short code - it works on VC++ Express but not on Bloodshed!!

[edit]Nevermind[/edit]
Last edited by John A; Jan 14th, 2007 at 9:41 pm.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jan 14th, 2007
0

Re: Short code - it works on VC++ Express but not on Bloodshed!!

I'm surprised that it even compiled on VC++ with a mistake like this:
C++ Syntax (Toggle Plain Text)
  1. cin.get()
  2. }
You also might want to read this:
http://www.sgi.com/tech/stl/count_if.html

I'm using g++, and it supports neither count() nor count_if().

That was my mistake when pasting the code and editing the code tags, sorry for any confusion .


And yes, that code works, mind to explain the structure please?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
fesago90 is offline Offline
21 posts
since Jan 2007
Jan 14th, 2007
1

Re: Short code - it works on VC++ Express but not on Bloodshed!!

Click to Expand / Collapse  Quote originally posted by fesago90 ...
And yes, that code works, mind to explain the structure please?
Hmm, I'm not sure I can explain it in a clear way because this is a subtle and tricky problem. Put simply, my hunch was that the problem came from your use of isdigit() directly as a template argument type.

If your compiler declares a library function with C linkage, like isdigit() in this case, you can't use it as a template argument type because functions with C++ linkage are expected. The linkage difference includes things like name mangling that make a function with C linkage hard to use safely, so I guess they just don't allow it. My solution was to create a function object with guaranteed C++ linkage and have it return a call to isdigit().

There's nothing special about a structure, anything that forces C++ linkage to a function-like thing works just as well. You could also do it with just a regular C++ function for example.
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int predicate( int x )
  8. {
  9. return isdigit( x );
  10. }
  11.  
  12. int main()
  13. {
  14. string foo = "123";
  15.  
  16. if(count_if(foo.begin(), foo.end(), predicate) == foo.size())
  17. {
  18. cout << "\nGreat!\n";
  19. }
  20.  
  21. cin.get();
  22. }
In a perfect world, the reason VC++ worked fine is because isdigit() is declared with C++ linkage for that example. Here in the real world, VC++ probably just got it wrong.

Oh, and isdigit() is declared in <cctype>, not <cstdlib>.
Reputation Points: 84
Solved Threads: 15
Posting Whiz in Training
Ravalon is offline Offline
209 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ dvd shop
Next Thread in C++ Forum Timeline: Setprecision help please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC