15

I've been looking for an answer on websites but couldn't find any answer that helped me.

I have a code that uses strings when i tried (like suggested) to add these lines:

using namespace std;
using std::string;
#include <string>

I tried to use each of them seperately and I tried all of them together. The best situation was when all of the string errors disappeared but I had another weird error on the line "using std::string" and the error was: std::string has not been declared. Any ideas? Thanks guys.

Richard D
  • 317
  • 3
  • 16
wannabe programmer
  • 643
  • 1
  • 8
  • 22

2 Answers2

14

Put #include <string> first.

Avoid using statements in headers as you potentially bring in all sorts of stuff into many compilation units. using std::string is perhaps acceptable in a header but using namespace std certainly isn't as it will cause so much namespace pollution in all compilation units. The std namespace is continuously expanding (look at all the new stuff in C++), so you don't want to have to fix lots of errors when upgrading your compiler.

Bathsheba
  • 227,678
  • 33
  • 352
  • 470
  • I don't think `using std::string` is acceptable in a header, *unless* it is in a limited scope. – juanchopanza Jun 11 '13 at 09:03
  • @RoeeGavirel I know. But this answer explicitly speaks about headers. – juanchopanza Jun 11 '13 at 09:05
  • @juanchopanza; I agree with you (it's one of the standards I enforce). – Bathsheba Jun 11 '13 at 09:08
  • 1
    Some possibly helpful terminology: `using namespace std;` is a using directive. `using std::string;` is a using declaration. – Luc Danton Jun 11 '13 at 09:10
  • @juanchopanza: I use it along with vector and plenty other using declarations in the common header that is forced include for all projects. With the best results. Using std:: on exotic stuff is tolerable, but otherwise is is just utter noise. Do you allow reusing name 'string' for other purposes? – Balog Pal Jun 11 '13 at 09:24
  • @BalogPal I wouldn't allow to re-use `string` or `vector` or many other names, but people do it anyway. The number of times I've seen things called `list`, `map` or `set`... – juanchopanza Jun 11 '13 at 09:43
  • so don't let people do it anyway... how hard it is to spot on a review really? – Balog Pal Jun 11 '13 at 09:46
2

The include should come before the using

#include <string>
using namespace std;
//using std::string; <-- Needless
Roee Gavirel
  • 18,150
  • 12
  • 61
  • 88
  • 1
    Why encourage someone to say `using namsepace std`? – juanchopanza Jun 11 '13 at 09:00
  • @juanchopanza (a) because it's a valid supported possibility. (b) I use it when I know what I'm doing and it never cause me any problem in the last 8 years... – Roee Gavirel Jun 11 '13 at 09:02
  • Every day there is some SO post where someone has a problem that comes from `using namespace std`. – juanchopanza Jun 11 '13 at 09:04
  • @juanchopanza what's the alternative? – wannabe programmer Jun 11 '13 at 09:05
  • @juanchopanza Every day there are car accidents, so you would advice people to stop driving anymore? – Roee Gavirel Jun 11 '13 at 09:06
  • 1
    @wannabeprogrammer you can refer to the string as `std::string` when required, or say `using std::string` in a limited scope, for example in a function or a class. – juanchopanza Jun 11 '13 at 09:07
  • 1
    If I could see that some of these accidents are causeb by people driving blindfolded, I would at least make sure I don't suggest to learners to drive blindfolded. – juanchopanza Jun 11 '13 at 09:08
  • @juanchopanza: You mean without adding the //so sue me comment a'la Scott Meyers? ;-) The recent popular guideline is still like "not in header, fair game in .cpp file" – Balog Pal Jun 11 '13 at 09:27
  • @BalogPal I wouldn't consider it fair game in a cpp file, unless it is in a limited scope. Of course, if it is only you who is maintaining the .cpp, then you can do whatever you like. – juanchopanza Jun 11 '13 at 09:41
  • @juanchopanza: it's your fair choice. I just made a note that the alternative is also popular and accepted by top mentors – Balog Pal Jun 11 '13 at 09:47
  • see now that i have encouraged a teaching discussion this post deserves a higher vote ;) – wannabe programmer Jun 11 '13 at 09:50