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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2007
Posts: 21
Reputation: fesago90 is an unknown quantity at this point 
Solved Threads: 1
fesago90's Avatar
fesago90 fesago90 is offline Offline
Newbie Poster

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

 
0
  #1
Jan 14th, 2007
  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:
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.
http://www.ulteo.com
Ulteo - Taste a bit of freedom
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

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

 
0
  #2
Jan 14th, 2007
I'm surprised that it even compiled on VC++ with a mistake like this:
  1. cin.get()
  2. }
You also might want to read this:
http://www.sgi.com/tech/stl/count_if.html

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().
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

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

 
0
  #3
Jan 14th, 2007
Originally Posted by fesago90 View Post
  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?
  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.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

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

 
0
  #4
Jan 14th, 2007
[edit]Nevermind[/edit]
Last edited by John A; Jan 14th, 2007 at 9:41 pm.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 21
Reputation: fesago90 is an unknown quantity at this point 
Solved Threads: 1
fesago90's Avatar
fesago90 fesago90 is offline Offline
Newbie Poster

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

 
0
  #5
Jan 14th, 2007
Originally Posted by joeprogrammer View Post
I'm surprised that it even compiled on VC++ with a mistake like this:
  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?
http://www.ulteo.com
Ulteo - Taste a bit of freedom
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

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

 
1
  #6
Jan 14th, 2007
Originally Posted by fesago90 View Post
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.
  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>.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC