2 minute read

Introduction

The std::exp function is a useful function that returns the exponential (Euler’s number) raised to a certain argument for our C++ program. However, we can often get errors from the compiler such as:

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

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

Potential causes

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

Fix #1: Add cmath to your depedencies

Essentially, the std::exp 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() {
	double x = 4.52, result;
	
	result = std::exp(x);
	std::cout << "e^x is " << result << std::endl; //Returns 91.8356

    return 0;
}

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

Fix #2: Using namespace std

Note that we have previously typed: std::exp instead of exp. We can type “exp” 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 exp (which is obviously shorter) instead of std::exp. For instance, we can have something like:

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

int main() {
	double x = 4.52, result;
	
	result = exp(x);
	cout << "e^x is " << result << endl; //Returns 91.8356

    return 0;
}

It is okay to type std::exp without typing “using namespace std”. In fact, it is generally recommended to type the full std::exp 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 exp instead of std::exp, then you need to add “using namespace std” to the header.

Conclusion

In this article, we have shown you how to fix the exp is not a member of std error by including the cmath library, using the std::exp function and by including “using namespace std” to the header. Always check that you are using the correct library, function name and namespace.

References

Read more about the std::exp function here: https://en.cppreference.com/w/cpp/numeric/math/exp