If you changed the extension of the source file to .cpp instead of .c, what happens? What version of g++ are you using?
Thanks Paul for your prompt reply. Here is the output:
Code:
$ mv strToLower.c strToLower.cpp
$ g++ -g -Wall strToLower.cpp -o strToLower
strToLower.cpp: In function ‘int main(int, char* const*)’:
strToLower.cpp:10: error: no matching function for call to ‘transform(__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> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)’
$ g++ --version
i686-apple-darwin9-g++-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5490)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I just also tried on my remote Red Hat server, and I got error as well:
Code:
$ g++ -g -Wall strToLower.cpp -o strToLower
strToLower.cpp: In function 'int main(int, char* const*)':
strToLower.cpp:10: error: no matching function for call to 'transform(__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> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unresolved overloaded function type>)'
$ g++ --version
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
It looks like you have a bad install, your #include path is not correct, or the compiler just doesn't have that function (which is hard to believe).
If you opened the <algorithm> file, do you see a transform function there? If you do, can you post the declaration of it (it will be a template function, so all we want is the declaration, not the entire body of the function).
Well, the problem is that the version of std::tolower inherited from the C standard library is a non-template function, but there are other versions of std::tolower that are function templates, and it is possible for them to be included depending on the standard library implementation. You actually want to use the non-template function, but there is ambiguity when just tolower is provided as the predicate.
One workaround is to qualify the name since the non-template function may be available in the global namespace:
But it may be better to define a wrapper for tolower that actually takes a char argument and returns a char (the version inherited from C takes an int argument and returns an int).
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
Well, the problem is that the version of std::tolower inherited from the C standard library is a non-template function, but there are other versions of std::tolower that are function templates, and it is possible for them to be included depending on the standard library implementation.
Adding "::" solved the problem. Thank you very much for your help, Paul and laserlight.
Sorry Paul, would you mind trying the code with "::" added? The reason I ask is because if that does not work with your compiler (Corneau), then I really doubt the portability of C++ code!
Sorry Paul, would you mind trying the code with "::" added? The reason I ask is because if that does not work with your compiler (Corneau), then I really doubt the portability of C++ code!
However, as I implied, there is no guarantee that the non-template version of tolower will be available in the global namespace (unless you #include <ctype.h> instead). A really portable solution is to write a wrapper, e.g.,
Thanks for the link. I tried both with and without "::" and they both worked.
Originally Posted by laserlight
However, as I implied, there is no guarantee that the non-template version of tolower will be available in the global namespace (unless you #include <ctype.h> instead). A really portable solution is to write a wrapper, e.g.,
Thanks for the link. I tried both with and without "::" and they both worked.
Yeah, this one is much better. By the way, I tried also with and without "inline" and they both worked. Do I really need that "inline"?
Not really. It's used as a request to the compiler to inline the code, but the actual inlining doesn't have to be granted.
Bookmarks