2 minute read

Introduction

The std::ceil function is a useful function that returns the number rounded upwards for our C++ program. However, we can often get errors from the compiler such as:

error: ‘ceil’ is not a member of ‘std’

This error is pretty straight-forward to fix as we will see below.

Potential causes

The ceil is not a member of std error could be caused by multiple reasons.

Fix #1: Add iostream to your depedencies

Essentially, the std::ceil function needs to have access to the cmath module in order to be executed by the compiler.

Therefore, you must add the following #include header to the top of your code (in the include(s) part) such as:


#include <cmath> //Add this
#include <iostream>

int main() {
    float x = 21.14;
    std::cout << std::ceil(x) << std::endl; //Returns 22

    return 0;
}

The compiler should now recognize the std::ceil function, thus fixing the ceil is not a member of std error.

If the std error still persists, keep reading below.

Fix #2: Using namespace std

Note that we have previously typed: std::ceil instead of ceil. We can type “ceil” only if we are declaring that we are using its namespace.

In other words, we would need to type “using namespace std” in the header if we only want to type ceil (which is obviously shorter) instead of std::ceil. For instance, we can have something like:


#include <cmath>
#include <iostream>
using namespace std; //Add this

int main() {
    float x = 21.14;
    cout << ceil(x) << endl; //Returns 22

    return 0;
}

It is okay to type std::ceil without typing “using namespace std”. In fact, it is generally recommended to type the full std::ceil function name (and therefore avoiding using namespace std) when working with multiple libraries because it can reduce future confusion.

However, if you still want to type “ceil” instead of “std::ceil”, then you need to add “using namespace std” to the header

References

Read more about the std::ceil function here: https://www.cplusplus.com/reference/cmath/ceil/