0

I am running g++(gcc version 3.4.4) on cygwin.

I can't get this small snippet of code to compile. I included the appropriate headers.

int main(){

    std::string temp("asgfsgfafgwwffw");

    std::transform(temp.begin(),
                   temp.end(),
                   temp.begin(),
                   std::toupper);

    std::cout << "result:" << temp << std::endl;

    return 0;
}

I have not had any issues using STL containers such as vector. Does anyone have any suggestions or insights into this situation. Thanks.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
Pradyot
  • 2,897
  • 7
  • 41
  • 58
  • Could you post the error messages as well? – avakar Aug 29 '09 at 05:49
  • what are "the appropriate headers", and which error are you getting? Your question might as well just be "I'm writing the correct code, but it doesn't work. What's wrong?" We need to know 1) what you're doing, and 2) what the error is. – jalf Aug 29 '09 at 13:28
  • There's a great explanation for this problem (and it's solution) [here](http://stackoverflow.com/a/7132065/1165522). – David C Aug 17 '12 at 21:20

2 Answers2

2

From the link above.

#include <cctype> // for toupper
#include <string>
#include <algorithm>
using namespace std;

void main()
{
string s="hello";
transform(s.begin(), s.end(), s.begin(), toupper);
}

Alas, the program above will not compile because the name 'toupper' is ambiguous. It can refer either to:

int std::toupper(int); // from <cctype>

or

template <class chart> 
  charT std::toupper(charT, const locale&);// from 
  <locale>

Use an explicit cast to resolve the ambiguity:

std::transform(s.begin(), s.end(), s.begin(), 
               (int(*)(int)) toupper);

This will instruct the compiler to choose the right toupper().

0

This explains it quite well.

Which will boil down to this code:

std::transform(temp.begin(),temp.end(),temp.begin(),static_cast<int (*)(int)>(std::toupper));
Ryan
  • 26
  • 3
  • 3
    please explain the problem *in* the post. External articles may be taken down, and then your answer will be useless. Plus it's a pain for all of us to have to follow an external link just to know whether your answer is correct or not. – jalf Aug 29 '09 at 13:29
  • I apologise for having missed this obvious solution. – Pradyot Aug 29 '09 at 18:22