命名空间std中没有名为'function'的类型。

4

我希望能够传递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

4
这个说法太笼统了,因为有无数个原因会导致你的图书馆变得陈旧。 - Puppy
你能包含完整的cpp文件吗? 你能包含你使用的全部命令行参数吗? 你正在运行哪个版本的OS X?没有这些信息,我们无法重现您的问题,这使得帮助您非常困难。 - Bill Lynch
1
所以,因为你使用的是OS X操作系统,你基本上有两个选择。升级到更高版本的OS X,或者按照这里的说明(https://dev59.com/mHXYa4cB1Zd3GeqP4EBo)尝试在你的系统上安装一个更新的libc++版本。 - Bill Lynch
@Jeffrey 先生,您真是个天才。它起作用了。 - 11684
1
您不应该在问题正文中放置答案。但是,您可以接受现有答案之一。 - Shoe
显示剩余10条评论
2个回答

12

您忘记了:

#include <functional>

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

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

1
实际上,我两个都有。 - 11684
4
请提供更多细节,例如完整的cpp文件和编译时使用的参数,这样做会很棒。 - Bill Lynch
1
@11684 所以显然,你两个都没有。 - Shoe
2
@Jeffrey O:) (公平地说,我的评论是在你编辑-stdlib标志之前发表的。) - 11684
1
哎呀,我以为我能读懂。显然,我需要重新学习一些小学的知识。(回想起来,我的第一条评论不仅无知,而且很粗鲁。那并不是我的本意。) - 11684
显示剩余3条评论

0

您需要一个C++11库和#include <functional>,以及一个C++11编译器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接