Do I Have to Use #Include <String> Beside <Iostream>

Do I have to use #include string beside iostream?

Yes, you have to include what you use. It's not mandated that standard headers include one another (with a few exceptions IIRC). It might work now, but might fail on a different compiler.

In your case, apparently <iostream> includes <string>, directly or indirectly, but don't rely on it.

When do I use '#include string' at the start of a C++ program?

Do I only use #include <string> if a variable represents a string?

Yes.

Use #include <string> when you use a variable that has type std::string.

The code "text here", contrary to intuition, is not a std::string; it is a string literal, and a C-style string, and a const char[10] convertible to const char*. Welcome to C++ with its legacy oddities.

When to include the string header in your program?

Your teacher was correct.

Your program worked without <string> by chance. Your standard library implementation, of that version, on that platform, under those circumstances, on that day, transitively included what you needed via <iostream>. The standard library is just code, like yours, and it just so happens that your particular implementation contains, inside <iostream>, an #include <string>. It could be buried behind many other #includes but got there eventually. But that's honestly pure chance, and does not mean that this is something the language guarantees, or something that must always be the case even in practice.

You should always code to standards.

If you're using features from <string>, include <string>.

Just today I was trying to build my big project with a new toolchain and found a few places where I'd accidentally relied on transitive includes, and it broke the build as a result because the new standard library implementation had a slightly different arrangement of headers. I dutifully added the missing #includes and now the world is a better place for it.

C++ Successful compilation without inclusion of string header

Shouldn't it be giving me an error?

It's implementation dependent. Some compiler implementations intrinsically #include <string> with the <iostream headers, others have forward declarations.

Always include the headers for any standard types you use. Multiple #include statements do no harm, and will be optimized using the appropriate header guards already.

C++ use iostream inside class

Why I can't use iostream and cout?

Because a class cannot (directly) contain expression statements. It can contain only member declarations.

Expression statements can only be within functions. This would be correct for example:

class main {
map<string, string> Map;

void example_function() {
Map["Ziv"] = "Sion";
cout << Map["Ziv"];
}
};

Which is the correct way to use a header file in C++?

Should I put my all the header files that example.cpp is gonna need in the header example.hpp or should I keep them in the example.cpp like the example above?

Keep the header file minimal.

The header file provides a function declaration. The function declaration depends on things in <string> but not things in <iostream>. So the header file should include <string> without <iostream>.

If the header file includes unneccessary things, the user of the header file will include those unneccessary things too.

How the C++ will work in this situation: I've included 'string' and 'iostream' in both main.cpp and example.cpp... Is the C++ compiler gonna link (or whatever term you'll find more qualified) the program two times with the string and iostream headers? should I put the string and iostream headers only in the example.cpp

C++ sources does not link with headers. Whenever you write #include <string>, the preprocessor (conceptually) copy the entire header file named "string" and paste at the line of #include <string>. Preprocessing occurs before compilation and linking.

And finally, should I put the namespaces that I'm gonna use for my example header in the header itself (example.hpp) or in the implementation (example.cpp)?

Never write using namespace std; in the global scope of the header file. Due to the copy-and-paste nature of #include, the using-directive will infect other files that include the header. What if there is a file that wants to use say_something but does not want to using namespace std?

c++ cout and cin doesn't compiles

operator>> overloads for string are located in string header, so you should include it:

#include <string>


Related Topics



Leave a reply



Submit