【问题标题】:No type named 'function' in namespace std [closed]命名空间std中没有名为'function'的类型[关闭]
【发布时间】:2014-09-21 07:54:03
【问题描述】:

我想传递 lambda,所以我定义了一个这样的函数:

double getInput(std::string inputDescription, std::function<bool(double)> isValid) { ... }

但是 gcc 拒绝编译它。我很快了解到我需要一个支持 C++11 的编译器,所以我下载了带有 MacPorts 的 clang 3.5。我找到了 clang++ 并确认它是正确的版本(我不是不小心使用了原来的 clang 1.7):

$ /opt/local/bin/clang++ --version
clang version 3.5.0 (trunk 210448)
Target: x86_64-apple-darwin10.8.0
Thread model: posix

但即使是 Clang 3.5 也给了我:

tempConverter.cpp:14:52: error: no type named 'function' in namespace 'std'
double getInput(std::string inputDescription, std::function<bool(double)> isValid) {
                                              ~~~~~^

完整的 .cpp 文件:

#include <iostream>
#include <cmath>
#include <iomanip>
#include <functional>
#include <string>

static const double minTemp = -273.15;
static const double maxTemp = 500.0;

inline bool between(double x, double min, double max) {
    return min <= x && x <= max;
}

double getInput(std::string inputDescription, std::function<bool(double)> isValid) {
    double input;
    std::cout << inputDescription << std::endl;
    std::cin >> input;

    while (!isValid(input)) {
        std::cout << "Invalid input. Please reenter." << std::endl;
        std::cin >> input;
    }

    return input;
    /*double temp1, temp2;
    std::cout << "Please enter consecutively the upper and lower limits, both between " <<  MIN_TEMP << " and " << MAX_TEMP << "." << std::endl;
    std::cin >> temp1;
    std::cin >> temp2;
    while (!between(temp1, MAX_TEMP, MIN_TEMP) || !between(temp2, MAX_TEMP, MIN_TEMP)) {
        std::cout << "At least one of the temperatures is out of bounds. Please reenter:" << std::endl;
        std::cin >> temp1;
        std::cin >> temp2;
    }
    upper = std::max(temp1, temp2);
    lower = std::min(temp1, temp2);
    std::cout << "Please enter a positive stepsize, smaller than the difference between the limits." << std::endl;
    std::cin >> step;
    while (step < 0 || step > upper - lower) {
        std::cout << "The stepsize is out of bounds. Please reenter:" << std::endl;
        std::cin >> step;
    }*/
}

double toFahrenheit(double celsius) {
    return celsius*(9.0/5.0) + 32;
}

void printTable(double start, double end, double step) {
    std::cout << std::setw(10) << "Celsius" << "|" << std::setw(10) << "Fahrenheit" << std::endl;
    std::cout << std:setw(10) << "=======" << "|" << std::setw(10) << "==========" << std::endl;
    for (double i = start; i < end; i += step) {
        std::cout << std::setw(10) << i << "|" << std::setw(10) << toFahrenheit(i) << std::endl;
    }
}

int main() {
    double start = getInput("Please enter the upper limit.", [](double x) { return between(x, minTemp, maxTemp); });
    double end = getInput("Please enter the lower limit.", [&](double x) { return x < start && between(x, minTemp, maxTemp); });
    double step = getInput("Please enter the stepsize.", [&](double x) { return x < end - start && x > 0; });
    printTable(start, end, step);
}

编译为:

/opt/local/bin/clang++ -std=c++11 tempConverter.cpp -o tempConverter

【问题讨论】:

  • 即使您已经下载了较新的编译器,您肯定仍然在使用较旧的 c++ 标准库。
  • 啊,应该是这样的。是否可以更新您的标准库?
  • 这太宽泛了,因为有 9999 个理由可以让你拥有一个旧图书馆。
  • 所以,因为您使用的是 OS X,所以您基本上有 2 个选择。升级到更新版本的 os x,或按照说明here,尝试在您的系统上安装更新版本的 libc++。
  • 您不应该将答案放在问题正文中。但是,您可以接受现有答案之一。

标签: c++ c++11 lambda clang clang++


【解决方案1】:

你忘了:

#include <functional>

或者你忘记了 C++11 标志:

-std=c++11 -stdlib=libc++

【讨论】:

  • 其实我没有。我两个都有。
  • @11684:在您的原始问题中包含这样的内容会很棒。也许是一个完整的 cpp 文件?以及你用来编译它的参数。
  • @11684 显然,你没有两者兼得。
  • @Jefffrey O:) (公平地说,我的评论早于您编辑 -stdlib 标志。)
  • 天啊,我以为我可以阅读。显然,我需要修改一些小学材料。 (回想起来,我的第一条评论似乎不仅无知而且粗鲁。这不是故意的。)
【解决方案2】:

您需要一个 C++11 库和#include &lt;functional&gt; 以及一个 C++11 编译器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 2014-12-21
    • 2020-04-21
    • 2013-05-27
    相关资源
    最近更新 更多