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.

12
  • Have you tried compiling it? Commented Jul 11, 2016 at 3:06
  • What is #includes? Commented Jul 11, 2016 at 3:09
  • Please try compiling it on your own. I compiled it on eclipse and encountered no error. PS, I have suggested an edit to your post. Use #include, not #includes. Also, please use using namespace std; or std:: Commented Jul 11, 2016 at 3:09
  • 3
    @VaibhavBajaj You did not compile the code that was posted. It is riddled with errors. Commented Jul 11, 2016 at 3:10
  • 2
    @VaibhavBajaj You are changing the OP's question. The code posted does not compile correctly. Commented Jul 11, 2016 at 3:11

4 Answers 4

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.

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

7 Comments

you need to use <string> not <string.h>
Depends on the compiler being used. <string.h> is used in old compilers like Turbo C++
If i used <code>using namespace std;</code> would I be able to get rid of the std:: in std::string?
Yes, you would though that is not recommended as sometimes you use multiple namespaces
Using statements like using std::string; using std::cout;, etc would be safer than using a single using namespace std; statement.
|
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.

3 Comments

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.
@VaibhavBajaj: I believe you did the wrong thing of editing it. This has invalidated the original question and nevertheless made the answers irrelevant.
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.
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.

Comments

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.