【问题标题】:Error C3861 in Visual with std::transform带有 std::transform 的 Visual 中的错误 C3861
【发布时间】:2012-04-01 12:52:44
【问题描述】:

我有点好奇为什么我用mac在Xcode上写的程序运行良好,但是当我尝试用visual studio在windows系统上编译时,

我收到以下错误:

c:\users\bryan\documents\visual studio 2010\projects\new\new\new.cpp(172):错误 C3861:“转换”: 未找到标识符。

实际上,当我在程序中的任何位置编写transform 时,它会传达相同的信息,就好像transform 不是std 命名空间的一部分一样。如果您想亲自查看,这是我的代码:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <cctype>
using namespace std;
string getInput ();
ifstream * openInFile ();
int getShiftValue ();
void cypherMenu ();
void menu ();
string shiftCharacters (int shiftNum, ifstream * inFile);
string getOutput ();
ofstream * openOutFile ();
void printSentence (string outData, ofstream * outFile);
void notOption (string optionString);
string capitalize (string choice);
string option ();
int main() {
ifstream * inFile;
ofstream * outFile;
string inFileName, outFileName, outData, optionString, capOptionString; 
int shiftNum = 0;
bool isOption = false; 
while (capOptionString.compare("2") != 0 || 
capOptionString.compare("QUIT") != 0) {
do {
    menu();
    optionString = option();
    capOptionString = capitalize(optionString);
    if (capOptionString.compare("1") == 0 || capOptionString.compare("CAESAR")
        == 0) {
        isOption = true;
    }
    else if (capOptionString.compare("2") == 0 || 
            capOptionString.compare("QUIT") == 0) {
            isOption = false;
    return 0;
    }
    else {
    notOption(optionString);
    }
}
while (!isOption);
cypherMenu();
inFile = openInFile(); 
shiftNum = getShiftValue();
outData = shiftCharacters(shiftNum, inFile);
inFile->clear();
inFile->close();
outFile = openOutFile();
printSentence(outData, outFile);
outFile->clear();
outFile->close();
}
return 0;
}
// Input Functions
string getInput () {
cout << "Enter an input file name: "; 
string inFileName;
getline(cin, inFileName); 
return inFileName;
}
string getOutput () {
string outFileName;
cout << "Enter an output file name: ";
getline(cin, outFileName);
cout << endl;
return outFileName;
}
ifstream * openInFile () {
ifstream * inFile; 
bool isGood = false; 
string inFileName; 
inFile = new ifstream;
do { 
    inFileName = getInput();
    inFile->open(inFileName.c_str());
    if (inFile->fail()) { 
    cout << "Couldn't open file" << endl;
    }
    else {
    isGood = true;
    }
}
while (!isGood);
return inFile;
}
ofstream * openOutFile () {
ifstream testStream; 
ofstream * outFile; 
bool isUnique = false; 
string fileName;
do { 
    fileName = getOutput();
    testStream.clear(); 
    testStream.open(fileName.c_str(), ios_base::in);
    if (testStream.good()) {
        cout << "The file already exists, please choose another" 
        << endl;
        testStream.clear();
        testStream.close();
    }
    else {
        isUnique = true;
        testStream.clear();
        testStream.close();
    }
}
while (!isUnique);
outFile = new ofstream;
outFile->open(fileName.c_str());
return outFile;
}
int getShiftValue () {
int shiftNum;
string trash;
cout << "Please enter shift value: ";
cin >> shiftNum;
getline(cin, trash); 
return shiftNum;
}
string option () {
string optionString;
getline(cin, optionString);
cout << endl;
return optionString;
}
// Data manipulation functions 
string shiftCharacters (int shiftNum, ifstream * inFile){
string inData, outData, trash, tempString; 
char outChar;
int idx = 0, idxTwo = 0, newLines = 0;
stringstream outSentence; 
do {
    while (getline(* inFile, inData, '\n')) {
        for (idx = 0; idx <= inData.length() - 1; idx++) {
            if (inData[idx] >= 'a' && inData[idx] <= 'z') {
            outChar = (((inData[idx] - 'a') + shiftNum) % 26) +
            'a';
            outSentence << outChar;
            }
            else if (inData[idx] >= 'A' && inData[idx] <= 'Z') {
                outChar = (((inData[idx] - 'A') + shiftNum) %  26) +
                'A';
                outSentence << outChar;
            }
            else {
                outChar = inData[idx];
                outSentence << outChar;
            }
        }
        outSentence << '\n';
        newLines++;
    }
}
while (!inFile->eof());
for (idxTwo = 0; idxTwo <= newLines + 1; idxTwo++) {
    getline(outSentence, tempString); 
    outData.append(tempString);
    outData += '\n';
}
return outData;
}
string capitalize (string choice) {
string outString;
outString.resize(choice.length());
transform(choice.begin(), choice.end(), outString.begin(), ::toupper);
return outString;
}
// Output funcitons
void cypherMenu () {
cout << "C A E S A R C Y P H E R P R O G R A M" << endl
<< "========================================" << endl;
return;
}
void printSentence (string outData, ofstream * outFile) {
int idx = 0;
char outChar;
stringstream outString;
outString << outData;
for (idx = 0; idx <= outData.length() - 1; idx++) { 
    outChar = outString.get();
    outFile->put(outChar); 
}
}
void menu () {
cout << "Available Options: " << endl 
    << "1. CAESAR - encrypt a file using Caesar Cypher" << endl
    << "2. QUIT - exit the program" << endl << endl
    << "Enter a keyword or option index: ";
return;
}
void notOption (string optionString) {
cout << optionString << " is an unrecognized option, try again" << endl 
<< endl;
return;
}

问题是大写函数下的变换。它似乎根本不知道transform,我什至尝试在它之前写std::,但它说没有叫transform的成员。我的编译器有问题吗,或者自从我下载了新工作室后,甚至不再使用转换,或者这只是不好的做法?

【问题讨论】:

    标签: c++ transform


    【解决方案1】:

    正如您在阅读文档(例如 http://en.cppreference.com/w/cpp/algorithm/transform)中所发现的那样,std::transform 是在标准头文件 &lt;algorithm&gt; 中定义的,而您并未包括在内。

    【讨论】:

      猜你喜欢
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多