본문 바로가기

Robotics/Software Tech.

G++ 에서 STL의 transform을 사용할때 발생하는 문제점의 해결방법

윈도우에서 어플리케이션을 짜놓고 리눅스로 바꾸는 과정중에 STL의 transform 함수에서 문제가 발생했다.

transform(strJointtype.begin(), strJointtype.end(), strJointtype.begin(), (int(*)(int))tolower);

이 코드를 컴파일 하니..


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>)

위와같이 에러가 뜬다.
이것을 해결하기 위해 구글링을 해보니, 답이 바로 나온다.


답은 아래 웹페이지에 나온다.
http://osdir.com/ml/debian.devel.gcc/2002-04/msg00092.html 또는,
http://stackoverflow.com/questions/1350380/problems-using-stl-stdtransform-from-cygwin-g

설명하면,

int main(){ 
 
    std
::string temp("asgfsgfafgwwffw");
 
 
    std
::transform(temp.begin(),
 
                   temp
.end(),
 
                   temp
.begin(),
 
                   std
::toupper);
 
 
    std
::cout << "result:" << temp << std::endl;
 
 
   
return 0;
 
}

위의 코드를 linux g++(gcc 3.4.4)에서 컴파일하면 transform에서 에러가 난다는 것이다. 이유는 transform가 문제가 아니라, 인자로 들어가는 toupper 이라는 함수가 문제다. 이 toupper 함수가 ambiguous하기 때문에 명확하게 인자를 캐스팅해야 한다는 것이다.

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

위 코드처럼 말이다...