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?

8
  • 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. Commented Jan 16, 2017 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 <string>. The only time you can get away with avoiding other includes are with forward declarations. Commented Jan 16, 2017 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. Commented Jan 16, 2017 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. Commented Jan 16, 2017 at 12:11
  • 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 <string>. Also, your include section of the source acts as a kind of documentation about what you depend on and use. Commented Jan 16, 2017 at 12:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
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".
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.
@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.
@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 ...
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.