24

Trying to sort an array of Integers and after some googling, came across the solution using std::sort accompanied by this error: namespace "std" has no member "sort".

Just to disqalify any qualms that I'm not using the std namespace, here is my header:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
Binny Zupnick
  • 397
  • 1
  • 2
  • 8
  • 6
    You should never put `using namespace` in a header; not everyone who includes your header will want the global namespace polluted. – Mike Seymour Jan 10 '13 at 14:25
  • 1
    What is worse is that the global namespace is polluted by different things depending on what order header files where included in the past and future. – Yakk - Adam Nevraumont Jan 10 '13 at 15:08
  • You should never put `#include "stdafx.h"` in a header either. Although this is a very old problem. I am commenting for the purpose of helping future readers who see this question like I have 6+ years after it was asked. – drescherjm Oct 24 '19 at 16:58

1 Answers1

52

Add:

#include <algorithm>

as stated in the std::sort() reference page.

See Using std Namespace, Why is "using namespace std" considered bad practice? and many other questions on SO discussing using namespace std;.

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • This resolved my error _namespace "std" has no member "sort"_ while attempting to compile with gnustl_static on the Android NDK. The reference to std::sort works in XCode and Visual Studio, but for the NDK I needed to add the include from this answer. Thanks! – Cory Trese Jun 27 '16 at 16:41