Question

I wanted to pass lambdas around, so I defined a function like this:

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

But gcc refused to compile it. I quickly learned I needed a compiler with C++11 support, so I downloaded clang 3.5 with MacPorts. I located clang++ and confirmed it was the right version (and I wasn't accidentally using the original 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

But even Clang 3.5 gives me:

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

The full .cpp file:

#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);
}

Is compiled with:

/opt/local/bin/clang++ -std=c++11 tempConverter.cpp -o tempConverter
Was it helpful?

Solution

You forgot to:

#include <functional>

or you forgot the C++11 flag:

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

OTHER TIPS

You need a C++11 library and #include <functional> as well as a C++11 compiler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top