【问题标题】:'transform': is not a member of 'std'“转换”:不是“标准”的成员
【发布时间】:2019-07-21 21:04:05
【问题描述】:

这是我的代码

#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
    // su is the string which is converted to lowercase
    std::wstring su = L"HeLLo WoRld";

    // using transform() function and ::toupper in STL
    std::transform(su.begin(), su.end(), su.begin(), ::tolower);
    std::cout << su << std::endl;
    return 0;
}

它会产生以下编译错误。

1>c:\users\root\source\repos\consoleapplication7\consoleapplication7\consoleapplication7.cpp(14): error C2039: 'transform': is not a member of 'std'
1>c:\users\root\source\repos\consoleapplication7\consoleapplication7\consoleapplication7.cpp(14): error C3861: 'transform': identifier not found
1>c:\users\root\source\repos\consoleapplication7\consoleapplication7\consoleapplication7.cpp(15): error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::wstring' (or there is no acceptable conversion)
1>    0 Warning(s)
1>    3 Error(s)

我在这里做错了什么?

【问题讨论】:

  • std::transform 在标题&lt;algorithm&gt; 中声明,您需要#include
  • 使用std::wcout 作为std::wstring
  • 另外,为了 const 的正确性,尽可能(和适当)使用 cbegin() 和 cend()

标签: c++ visual-studio compiler-errors


【解决方案1】:

您需要包含标题。

添加:

#include <algorithm>

【讨论】:

  • 也使用 using namespace std;std::transform
  • 我也遇到了std::ranges::transform 的这个问题——它没有ranges 标头中定义,而是在algorithm 标头中定义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 2013-11-21
  • 2022-10-21
相关资源
最近更新 更多