命名空间std中没有名为“function”的类型

时间:2014-07-29 19:57:12

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

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

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

2 个答案:

答案 0 :(得分:8)

你忘了:

#include <functional>

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

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

答案 1 :(得分:0)

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