0

Is there any option to use std in a header file without using any #include statement? I have a class in a header file as following;

class Sequence{
private:
    std::string sequence;
    unsigned length;
public:
    /* Constructors */
    Sequence (void);
    Sequence (std::string, unsigned);
    Sequence (const Sequence &);

    /* Destructor Definition */
    ~Sequence(){}

    /* Overloaded Assignment */
    Sequence & operator
        = (const Sequence &seq)
    {
        sequence = seq.sequence;
        length = seq.length;
        return *this;
    }

    /* Setter and Getter Functions */
    void setSequence(std::string);
    void setLength(unsigned);
    std::string getSequence(void);
    int getLength(void);
};

It is not compiled correctly without including iostream. However, I read some comments in related questions where we should not include libraries AND another header files in a header file. So?

  • You have two choices: Either include the header files you need in your header files. Or make sure the dependencies are included in the source file before you include your header file. Which one is best? Both are equally good, which one you pick is up to personal preference. – Some programmer dude Jan 16 '17 at 12:06
  • 1
    *"we should not include libraries AND another header files in a header file"* That advice does not apply to the standard library. You *must* include headers that you use in this case `#include `. The only time you can get away with avoiding other includes are with [forward declarations](https://stackoverflow.com/questions/4757565/what-are-forward-declarations-in-c). – Cory Kramer Jan 16 '17 at 12:07
  • 1
    If you use `std::string` in your interface, it is not equaly good to include it in a source file that's using it, because you will have to make sure it's included every time you use the header. That includes client code. – DrPepperJo Jan 16 '17 at 12:09
  • 4
    @SomeProgrammerDude *"Both are equally good"* Hardly. One requires me to carefully order my include statements, often digging through multiple layers trying to figure it out. **Header files should be fully independent,** including all of the header files for types they reference. This is why we have include guards. – Jonathon Reinhart Jan 16 '17 at 12:11
  • @CoryKramer Indeed, the code is compiled correctly with only including iostream. Without iostream, it does not work with only including string.h. At that point, I have two questions. The first one is do I have to include also string.h even if the code works correctly with only iostream? The second question is that it is really normal including iostream in a header file from your point of view? –  Jan 16 '17 at 12:12
  • 1
    @omerfaruk Some system header files may include other system header files, but it's an implementation specific thing. If you need `std::string` then you should include ``. Also, your include section of the source acts as a kind of documentation about what you depend on and use. – Some programmer dude Jan 16 '17 at 12:13
  • @JonathonReinhart In this scenario, how I can make this header file fully independent? Indeed, each header file consists of any string object can NOT be fully independent? There is one more option to do it fully independent is using char * pointers instead of string objects.However, when I tried this method, I was not able to struggle with dynamic memory operations correctly. –  Jan 16 '17 at 12:16
  • I say independent meaning it can be `#include`d by itself. It's OK if it includes other header files, but I should not have to include them before it. In this case you clearly need to `#include `. – Jonathon Reinhart Jan 16 '17 at 12:19

2 Answers2

5

If in your own header A you depend on type definitions, or function declarations from another header B (be it a standard header or otherwise) then you must include the header B in your header A.

In general you cannot avoid it. There are some cases, where you might only depend on a declaration of a type in which case you can avoid including the definition by using a forward declaration. However, this does not apply to the types from the standard library because those are in a namespace where you may not declare anything.

It is not compiled correctly without including iostream

Actually, your class definition depends on <string>. It may be possible that <iostream> includes <string>, but you cannot depend on that. Include <string> instead, because you use a type that is defined there (std::string).

we should not include libraries AND another header files in a header file.

You must include all headers that you depend on, whether they are from a library or not. There is nothing wrong with including both library headers, and other headers.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • I see. I have to include the libraries and header files I need to use in a header file. There is no another option, but does it affect the performance or another metrics of the code? –  Jan 16 '17 at 12:20
  • Actually, "must" in this context is defined as "you *really* should do that in order to preserve your sanity" rather than "your code won't work otherwise". – el.pescado - нет войне Jan 16 '17 at 12:21
  • Using char *pointers instead of string objects is better from memory and performance view? If I have many objects from a class, and want to store them in an array, should I define this array dynamically(with a pointer array) or in a static manner? I am new in C++, and want to know the professional metrics/methods at the beginning. –  Jan 16 '17 at 12:26
  • @omerfaruk if you depend on certain definitions or declarations, then there is no alternative to including those definitions and declarations. Since there is no alternative, there is nothing to compare performance against. What do you mean by "I have to include the libraries and header files"? You only ever include headers. A library may provide header files that you can use. If you have new unrelated questions, feel free to ask here on stackoverflow after you've studied the subject by yourself first. – eerorika Jan 16 '17 at 12:29
  • @user2079303 I am not asking any question before searching. Firstly, you did not cover my question about the methods used to define a new array of class objects? Secondly, I mean iostream and a class file as a library and a header file, respectively ... –  Jan 16 '17 at 12:37
  • @omerfaruk I didn't answer it because it's not relevant to your question about header files. As I said, feel free to post the question here on stackoverflow (as a question, not as a comment). To clarify, the iostream library is part of the standard library, and `` is a header provided by the iostream library. – eerorika Jan 16 '17 at 12:41
0

Sometimes you can use a forward declaration. Check out forward declarations for more information on that. In cases where you can't use a forward declaration, the needed header has to be included before the use in the translation unit. For example, if header A depends on header B, B has to be included before A uses it in the translation unit. There are two ways to achieve this:

  1. Include B on top of header A.
  2. Or: Everywhere where A is included, include B before A.

However, a header should be self-contained, so approach 1 is preferred.

haslersn
  • 515
  • 5
  • 10