0

So, this question will probably be shot down instantly, but, I recently started learning C++, and I noticed that there is a line of code that is:

#include <string>

Does this mean that if I had a program that stated:

#include <iostream>

using namespace std;

int main() {
    string foo = "test";
    cout << foo;
}

would it not compile correctly? And if so, would that mean that Strings have to be, in a more java-y way of saying it, "imported"? If there are any problems with the code below please let me know. Sorry if this was a bad question.

Dylan Black
  • 262
  • 3
  • 16

4 Answers4

1

You have to initialize a string by firstly #include<string> and then std::string nameofstring. Edit: for cout you also have to use std::cout if you don't want to use namespaces.

Paul Kramme
  • 111
  • 1
  • 9
1

would it not compile correctly? May or may not. It is an undefined behavior.

In C++, declaration should precede definitions and usage. So, if you are using a definition, compiler expects you to declare somewhere before you are using it.

General convention is to include all declarations inside a header file, and that is true for all types that are shipped with the standard library. If you are planning to use one of the standard library types, templates, constants or values, you should know from where that declaration would be present and include it in your translation unit failing which it would be an undefined behavior.

Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • I recently edited the code shown above. The previous version had no `using namespace std;` and was using `#includes`. Thus, it would not compile correctly. – Vaibhav Bajaj Jul 11 '16 at 03:25
  • @VaibhavBajaj: I believe you did the wrong thing of editing it. This has invalidated the original question and nevertheless made the answers irrelevant. – Abhijit Jul 11 '16 at 03:27
  • I did not know that such a great audience would come before my edit was approved. I am extremely sorry for all inconvenience caused heretofore. – Vaibhav Bajaj Jul 11 '16 at 03:28
1

It would be a mistake to equate #include with java's import: if you import a module, you have all of it's definitions and code. #includeing something doesn't guarantee you have everything. In C++ headers are often used to share definitions between modules, so that b.cpp knows how large class A is from a.cpp, and so forth. The implementation is stored in separate, compiled, binary files or libraries, and #include doesn't automatically associate those with your project.

Consider the following:

// foo.h
class Foo {
public:
    Foo();
};

// foo.cpp
#include <iostream>
#include "foo.h"
Foo::Foo() {
    std::cout << "hello\n";
}

"foo.h" contains the declaration of Foo, but it is lacking the functionality. If I now write another file:

// bar.cpp
#include "foo.h"

int main() {
    Foo f;
}

this compiles fine. But when it gets to the linking stage it will complain that I don't have the definition (implementation) of Foo::Foo(). You have to explicitly link the two binaries together to produce an executable. With GCC/Clang that would be

g++ -Wall foo.cpp bar.cpp

Or if you've turned "foo" into a library:

g++ -Wall bar.cpp -lfoo

With an IDE like Eclipse of Visual Studio, you would have to manually inform the project of the need to include the additional cpp file / object file / library.

kfsone
  • 23,617
  • 2
  • 42
  • 74
0

First of all, the originally shown code would not compile no matter what was #included. That's because the class name is std::string, and not string. As you've eventually figured out, and updated your question with.

Each class, or template, or any other resource that's defined by the C++ library requires an appropriate standard header file to be #included. Although including a particular header file could also, indirectly, include some other ones, so you end up getting those header files' contents "for free", this would be relying on a compiler-specific feature. When I regularly update my code and rebase against a newer version of gcc I often find some bits, here and there, that are now missing an explicit #include of the right header file, because, unknowingly, I missed it, but the previous compiler version's header files ended up pulling it in anyway.

So, after fixing the class name to std::string you may find out that the resulting code does or does not compile. If it does compile, it's only due to the good graces of your specific compiler's headers, and it may not be compilable by other compilers, or by newer versions of the same compiler you are using.

P.S. You should avoid using namespace std;, this is bad practice. As you've started learning C++, now would be the best opportunity to avoid acquiring bad programming habits.

Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148