Hi i just wanted to ask whether we cud use #include<string> and declare a data type called 'string' eg. string ch;? and where is "using namespace std" used?

Dani AI

Generated

Short answer: the C++ string type is provided by the <string> header and it lives in namespace std. After including the header you can either refer to the type with its namespace or bring the name into scope. covered the namespace concept well: prefer either qualifying the name or using a single-name using-declaration (for example, using std::string;) in .cpp files rather than a blanket using namespace std;.

A few important clarifications tied to the other replies: you can define your own type named string, but doing so in the global namespace while also using a using-directive for std can create ambiguous lookups and confusing compiler errors. was right that you can define your own class, and was right to suggest putting it in a separate namespace. Also note the small typo in 's post — a using-directive must end with a semicolon.

Troubleshooting and best practice: if the compiler complains that string is unknown, check that you included the <string> header and that you either qualify the name or have a using-declaration in the right scope. If you get ambiguity errors, remove the using-directive or move your type into a custom namespace. Avoid using namespace std; in header files because it pollutes the global namespace. For authoritative details see the C++ reference pages on the <string> header and on namespaces:

Recommended Answers

All 4 Replies

Hi i just wanted to ask whether we cud use #include<string> and declare a data type called 'string' eg. string ch;? and where is "using namespace std" used?

The Standard Template Directory (or Definitions... or Defined types...) is a namespace with defined C and C++ types/functions/objects.

Without the "using namespace std" you would have to make calls to the std's objects in this way--

//without the using namespace std

std::cout << "Making a call to the cout and endl objects from namespace std" << std::endl;

--and about your first question, I'm not sure if I'm answering your question correctly when I say this, but yes you can declare a variable of type String in that way... however I'd suggest that you use the call to the constructor of the string object in this way--

#include <string>

string ch = "MyString";

Yes, you can, but you can't use using namespace std; , i.e.,

#include <string>

std::string str = "whatever";
class string {
public:
  std::string str;
};
string q;

I think :P

Thanx guys for helpin me out.

Yes, you can, but you can't use using namespace std; , i.e., And you can even do the using namespace std; stuff.

#include <string>

std::string str = "whatever";
class string {
public:
  std::string str;
};
string q;

I think :P

That won't work because the compiler will get confused about whether you want std::string or the string class you wrote. But you can do it if you declare your own namespace

#include <string>
using namespace std:

namespace MyNamespace
{
     class string
     {
     private:
         std::string mystring;
     public:
         string() {};
         void setString(std::string str) { mystring = str; }
     };
};



int main()
{
    MyNamespace::string mystring;

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.