-4

On A.h i do not have to #include anything or use namespace for anything.

On the other header, B.h i have to include vector and using namespace std. Both of this headers are not linked. Seems to be a file problem. When i copy the contents of A.h (without any problems) to B.h, i still get a compiler error saying string is not a type name and vector is not a template although the compiler did not state anything when the same content was on A.h. I am coding c++ on visual studio. What is going on?

user859385
  • 607
  • 1
  • 6
  • 23
  • may i ask why the downvote? – user859385 Apr 06 '16 at 15:07
  • 5
    Please **[edit]** your question with a [mcve] or [SSCCE (Short, Self Contained, Correct Example)](http://sscce.org). Also please do not use `using namespace std;` in a header file. further reading: [Why is “using namespace std” in C++ considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice) – NathanOliver Apr 06 '16 at 15:08
  • yeah i did not use. I am saying. The compiler is giving me problems – user859385 Apr 06 '16 at 15:08
  • 4
    How can we tell you what the problem could be without seeing the code the compiler is trying to compile? – NathanOliver Apr 06 '16 at 15:09
  • 2
    @user859385 compiler doesn't *give you problems*. Your program is (probably) ill formed and the compiler tells you what's wrong with it. If you don't understand what the compiler says, then you can show us a [mcve] and we can explain it to you. – eerorika Apr 06 '16 at 15:14
  • 1
    Header files are usually not compiled. They are always included (i.e. expanded) in compilation units. Those compilation units may change the environment for the header. – IInspectable Apr 06 '16 at 15:14
  • 1
    Just an idea, but is it possible that there are include files that are always included before A.h and not before B.h. Normally, you should always include (or declare) what you need in a header file, so actually B.h is the one without any problems. – stefaanv Apr 06 '16 at 15:21

1 Answers1

1

Your header files should have #include <string> and #include <vector> among their first few lines. Then when you refer to those types either in your program or in the header file, I would recommend referring to std::string and std::vector. Using qualified references like that is the preferred alternative to using namespace std, as @NathanOliver pointed out.

Logicrat
  • 4,438
  • 16
  • 22
  • IMHO, header files should only `#include` header files *to resolve symbols in the header file*. Use `#include` in source files to resolve symbols in the source file. – Thomas Matthews Apr 06 '16 at 15:47
  • @ThomasMatthews I agree. I think my answer assumed the context that the header file in question did declare objects of top `string` and `vector`. – Logicrat Apr 06 '16 at 15:49