作用域中未声明的c++转换只发生在服务器上

C++ transform not declared in scope only occurs on server

本文关键字:服务器 转换 未声明 c++ 作用域      更新时间:2023-10-16

我见过很多,但似乎没有一个符合我的问题。这个程序在本地运行良好,但在服务器上运行时,我遇到了一个错误。

project1.cpp:在函数' void insertwords(char*) ':Project1.cpp:54:65:错误:' transform '没有在这个作用域中声明变换(word.begin (), word.end (), word.begin()::低),

相关代码:

void insertwords(char *filename) {
    ifstream fin;
    fin.open(filename);
        if(fin.fail())
            {
            cerr << "File opening failed. Exiting program.n";
            exit (-1);
            }
    string word;
    int count = 0;
    while (!fin.eof() ) {
        word.clear();
        fin >> word;
        transform(word.begin(), word.end(), word.begin(), ::tolower);
        for (int i = 0, len = word.size(); i < len; i++)
        {
            if(ispunct(word[i]))
            word.erase(i--, 1);
            len = word.size();
        }
        if(!word.empty()) {
            insert_word(word);
            ++count;
                }
        }

    cout << "The number of words found in the file was " << count << "n";
    fin.close();
}

包括:

#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
#include <locale>
using namespace std;

我知道使用命名空间std;这是不好的做法,但我被告知为项目

您需要#include <algorithm>,这是std::transform来自的标头。

至于为什么它会在一台机器上编译而不是另一台机器上,我的猜测是你的其他头文件之一(例如<string>)包括<algorithm>在一个编译器实现,所以你很幸运,但不是其他编译器。