# Log is not a member of std in C++

## Introduction

The `std::log`

function in C++ is a standard library function that calculates the natural logarithm (base e) of a given number. The function is defined in the `<cmath>`

header and can be used with both float and double data types. However, we can often get errors from the compiler such as:

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

This error can be fixed by including the cmath library and using the appropriate log function. In this article, we will show you how to fix the log is not a member of std error.

## Potential causes

The log is not a member of std error could be caused by not including the cmath library or not specifying the appropriate log function.

## Fix #1: Add cmath to your dependencies

The log function is part of the cmath library. 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() {
double x = 2;
std::cout << log(x) << std::endl; //Returns 0.693147
return 0;
}
```

The compiler should now recognize the log function and the error should be fixed.

## Fix #2: Using the log function with a specified base

If you want to calculate the logarithm of a number to a specific base, you can use the log function with a specified base such as log10 or log2. For example:

```
#include <cmath>
#include <iostream>
int main() {
double x = 100;
std::cout << log10(x) << std::endl; //Returns 2
return 0;
}
```

### Fix #3: Using namespace std

Note that we have previously typed: `std::log`

instead of log.
We can type “log” 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 log (which is obviously shorter) instead of `std::log`

. For instance, we can have something like:

```
#include <cmath>
#include <iostream>
using namespace std;
int main() {
double x = 2;
cout << log(x) << endl; //Returns 0.693147
return 0;
}
```

It is okay to type `std::log`

without typing “using namespace std”.
In fact, it is **generally recommended to type the full std::log function name** (and therefore avoiding using namespace std) when working with multiple libraries because it can reduce future confusion. Keep in mind that using namespace std is not recommended, as it might create naming conflicts when using other libraries.

However, if you still want to type `log`

instead of `std::log`

, then you need to add “using namespace std” to the header.

## Conclusion

In this article, we have shown you how to fix the log is not a member of std error by including the cmath library, and by specifying the appropriate log function. Always check that you are using the correct library, function name, and base of the logarithm. The log function is a powerful mathematical function that is widely used in various fields such as physics, engineering, and computer science. Understanding how to correctly use and implement the log function in your C++ program is essential for achieving accurate and efficient results.

It is also important to note that the log function has different variations such as log10, log2 and log1p. You can use these variations depending on your specific requirements.

## References

Read more about the `std::log`

function here: https://en.cppreference.com/w/cpp/numeric/math/log