Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No type named 'function' in namespace std [closed]

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
like image 616
11684 Avatar asked Aug 25 '26 04:08

11684


2 Answers

You forgot to:

#include <functional>

or you forgot the C++11 flag:

-std=c++11 -stdlib=libc++
like image 136
Shoe Avatar answered Aug 27 '26 17:08

Shoe


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

like image 36
Puppy Avatar answered Aug 27 '26 18:08

Puppy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!