g++抱怨STD功能

g++ complaining about STD functions

本文关键字:功能 STD 抱怨 g++      更新时间:2023-10-16

我有一段下载的源代码,当试图使用g++编译器通过Cygwin进行编译时,编译器给了我一个错误,说"transform"函数在此范围内未声明。。。

我使用的是std名称空间,并且我有正确的标题。我不知道为什么它没有编译。。语法看起来正确

这是代码块部分。

string tolower (const string & s)
  {
    string d = s;
    transform(d.begin(), d.end(), d.begin(), (int(*)(int)) tolower);
    return d;
  }  // end of tolower

这是我的标题部分:

#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
// standard library includes ...
#include <string>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <sstream>
#include <ios>
#include <iterator>
using namespace std; 

您需要为std::transform:包含适当的标头

#include <algorithm>

您还应该避免在头文件的全局命名空间中使用using namespace std;,这会污染包括头文件在内的任何代码的全局命名空间。

请参阅这篇关于using namespace std的文章。