+ 1
How can you use strings in C++?
No matter what I #include at the beginning of the code my strings arenât considered declared within the rest of my code. Any ideas on how to get them to work would be appreciated.
10 Answers
+ 2
@Keerthi
It tells the compiler to use the namespace we want, so that we do not have to scope all our keywords like this: std::cout, std::cin, etc.
After C++98, all headers were standardised, and were merged into a standard namespace. Thats why you no longer see the .h with their names.
Basically, the namespace was created to help avoid confusion. Now you may declare multiple variables with the same name inside different namespaces and you may call any one of them.
It is just a way to limit a scope of a variable.
Eg -
namespace alpha
{
int var;
};
namespace beta
{
int var;
};
int main()
{
using namespace std;
alpha::var = 2;
beta::var = 4;
cout<<beta::var<<endl; // Prints 4.
}
+ 4
Yes, as Forest suggests, first check if you are including the <string> library.
+ 3
#include <string>
+ 2
And do check if you have this:
using namespace std;
or this:
using std::string;
in your code, or your strings look like this:
std::string str;
+ 2
@Keerthi
This is C++, nothing else.
And yes, I'm just a school student, so, I don't think I should be addressed as sir...
// If that comment was meant for me, that is...
+ 2
@kinshuk vasisht what is the use of "using namespace ?"
+ 1
#include <string.h>
+ 1
which language is this Sir?
+ 1
thank you @kinshuk
0
Namespacesare used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.