Set is not a member of std in C++
Introduction
The std::set
function is a highly useful function that allows us to create and manipulate sets in the C++ programming language. However, we can often get errors from the compiler such as:
error: ‘set’ is not a member of ‘std’
This error is pretty straight-forward to fix as we will see below.
Potential causes
The set is not a member of std error could be caused by multiple reasons.
Fix #1: Add set to your dependencies
Essentially, the std::set
function needs to have access to the set 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 <set> //Add this
int main() {
std::set<int> mySet;
mySet.insert(1);
return 0;
}
The compiler should now recognize the std::set
function, thus fixing the set is not a member of std error.
Fix #2: Using namespace std
Note that we have previously typed: std::set
instead of set.
We can type “set” 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 set (which is obviously shorter) instead of std::set
. For instance, we can have something like:
#include <set>
using namespace std; //Add this
int main() {
set<int> mySet;
mySet.insert(1);
return 0;
}
It is okay to type std::set
without typing “using namespace std”.
In fact, it is generally recommended to type the full std::set
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 “set” instead of “std::set”, then you need to add “using namespace std” to the header.
References
Read more about the std::set
function here: https://www.cplusplus.com/reference/set/set/