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