No type named 'function' in namespace std

10,907

You forgot to:

#include <functional>

or you forgot the C++11 flag:

-std=c++11 -stdlib=libc++
Share:
10,907
11684
Author by

11684

Updated on June 04, 2022

Comments

  • 11684
    11684 almost 2 years

    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
    
  • 11684
    11684 almost 10 years
    Actually, I didn't. I got both.
  • Bill Lynch
    Bill Lynch almost 10 years
    @11684: It would be fantastic to include things like this in your original question. Perhaps a full cpp file? and the arguments you used to compile it.
  • Shoe
    Shoe almost 10 years
    @11684 So apparently, you didn't have both.
  • 11684
    11684 almost 10 years
    @Jefffrey O:) (To be fair, my comment predates you editing the -stdlib flag in.)
  • Shoe
    Shoe almost 10 years
    @11684 first revision says otherwise. :)
  • 11684
    11684 almost 10 years
    Gee, I thought I could read. Apparently, I need to revise on some elementary school material. (And in retrospect, my first comment seems not only ignorant but rude too. That wasn't intended.)
  • Shoe
    Shoe almost 10 years
    @11684 Apologies accepted. :)