Skip to content

/errors/cpp/no-member-named-in-namespace-std

C++: error: no member named 'setw' in namespace 'std' (gcc: 'setw' is not a member of 'std')

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
C++
exit code
1
versions
Clang, GCC, C++11 – C++23
os
any

error: no member named 'setw' in namespace 'std' (Clang) and error: 'setw' is not a member of 'std' (GCC) mean the compiler knows the std namespace but has not seen a declaration of that name in it. Almost always the header that declares it is missing (std::setw needs <iomanip>), or the name comes from a newer standard than the one you compile with (std::format needs -std=c++20). Include the header listed for the name on cppreference and pick the right -std.

Reproduce it

Clang 20 with its libc++ standard library, from the ziglang package on PyPI, run through uv. The same file is compiled as C++17 and as C++20:

bash
cat > app.cpp <<'EOF'
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v{3, 1, 2};
    std::cout << std::setw(4) << v.size() << "\n";
    std::cout << std::accumulate(v.begin(), v.end(), 0) << "\n";
    std::ranges::sort(v);
    std::cout << std::format("{}", v.size()) << "\n";
}
EOF
CXX="uv run -q --no-project --python 3.13 --with ziglang==0.15.1 python -m ziglang c++ -fno-color-diagnostics"
echo "== -std=c++17"; $CXX -std=c++17 -c app.cpp -o app.o
echo "== -std=c++20"; $CXX -std=c++20 -c app.cpp -o app.o

Output · exit 1

== -std=c++17
app.cpp:6:23: error: no member named 'setw' in namespace 'std'
    6 |     std::cout << std::setw(4) << v.size() << "\n";
      |                  ~~~~~^
app.cpp:7:23: error: no member named 'accumulate' in namespace 'std'
    7 |     std::cout << std::accumulate(v.begin(), v.end(), 0) << "\n";
      |                  ~~~~~^
app.cpp:8:10: error: no member named 'ranges' in namespace 'std'
    8 |     std::ranges::sort(v);
      |     ~~~~~^
app.cpp:9:23: error: no member named 'format' in namespace 'std'
    9 |     std::cout << std::format("{}", v.size()) << "\n";
      |                  ~~~~~^
4 errors generated.
== -std=c++20
app.cpp:6:23: error: no member named 'setw' in namespace 'std'
    6 |     std::cout << std::setw(4) << v.size() << "\n";
      |                  ~~~~~^
app.cpp:7:23: error: no member named 'accumulate' in namespace 'std'
    7 |     std::cout << std::accumulate(v.begin(), v.end(), 0) << "\n";
      |                  ~~~~~^
2 errors generated.

Under C++17, std::ranges and std::format do not exist at all. Under C++20 they are found, because libc++ happens to declare them through <vector> or <iostream>, while setw and accumulate still fail: their headers (<iomanip>, <numeric>) are never included. Relying on such indirect includes is fragile; another standard library (GCC's libstdc++, MSVC's STL) or another version can give a different result.

Fix it

Include every header you use, and compile with the standard the code needs:

bash
cat > app.cpp <<'EOF'
#include <algorithm>   // std::ranges::sort
#include <format>      // std::format (C++20)
#include <iomanip>     // std::setw
#include <iostream>
#include <numeric>     // std::accumulate
#include <vector>

int main() {
    std::vector<int> v{3, 1, 2};
    std::ranges::sort(v);
    std::cout << std::setw(4) << v.front() << "\n";
    std::cout << std::accumulate(v.begin(), v.end(), 0) << "\n";
    std::cout << std::format("{} items", v.size()) << "\n";
}
EOF
uv run -q --no-project --python 3.13 --with ziglang==0.15.1 python -m ziglang c++ -fno-color-diagnostics -std=c++20 app.cpp -o app && ./app

Output

   1
6
3 items

Names that often trigger it

NameHeaderMinimum standard
std::setw, std::setprecision, std::fixed<iomanip> (fixed: <ios>)C++98
std::accumulate, std::iota<numeric>C++98 / C++11
std::sort, std::find, std::max<algorithm>C++98
std::string, std::to_string<string>C++98 / C++11
std::unique_ptr, std::make_unique<memory>C++11 / C++14
std::optional, std::variant, std::filesystem<optional>, <variant>, <filesystem>C++17
std::format, std::ranges, std::span<format>, <algorithm> or <ranges>, <span>C++20
std::print, std::expected<print>, <expected>C++23

Sources