Declaring strings as std:string in C++

10,977

Solution 1

Writing the string declaration as std:string also works fine. What's the difference.

The difference would be slight clearer if you formatted it differently:

std:
    string c = "Test";

You're declaring a label called std, and using the name string which has been dumped into the global namespace by using namespace std;. Writing it correctly as std::string, you're using the name string from the std namespace.

If I use this std::string within a class to declare a private variable, I get an error error: ‘std’ does not name a type.

That's because you can't put a label in a class definition, only in a code block. You'll have to write it correctly as std::string there. (If the class is defined in a header, then using namespace std is an even worse idea than in a source file, so I urge you not to do that.)

Also, if you're using std::string, then you should #include <string>. It looks like your code works by accident due to <iostream> pulling in more definitions than it need to, but you can't portably rely on that.

Solution 2

You need to include the string class header:

#include <string>

This code has a typo, missing a second colon

std:string N;

should be:

std::string N;

With a single colon, it becomes a label for goto, which is probably not what you meant.

Solution 3

First problem:

First of all, you are missing the #include <string> directive. You cannot rely on other headers (such as <iostream>) to #include the <string> header automatically. Apart from this:

Second problem:

Writing the string declaration as std:string also works fine. What's the difference.

That is because you have an (evil) using directive at global namespace scope:

using namespace std;

The effect of this directive is that all names from the std namespace are imported into the global namespace. This is why the fully-qualified std::string and the unqualified string resolve to the same type.

If you omitted that using namespace std; directive, you would get a compiler error when using the unqualified string name.

Third problem:

If I use this std::string within a class to declare a private variable, I get an error error: ‘std’ does not name a type. Example of this declaration:

You are missing a colon. That should be:

std::string
//  ^

And not

std:string
// ^

Solution 4

Writing the string declaration as std:string also works fine. What's the difference.

There is no difference, unless you declare something else called string. In your code, string and std::string refer to the same type. But avoid using namespace std at all cost.

If I use this std::string within a class to declare a private variable, I get an error error: ‘std’ does not name a type. Example of this declaration:

You need to #include <string> in order to use std::string. What is happening is that in your first code sample, <iostream> seems to be including <string>. You cannot rely on that. You must include <string>.

Share:
10,977
itsols
Author by

itsols

Founder CEO of Marha Online (Pvt) Ltd - a solutions and training organisation. As a teacher, I mostly enjoy teaching the 9th to 12th grades. As a developer, I love taking on challenging solutions like mlti-site DB applications, client-server apps and web apps.

Updated on July 14, 2022

Comments

  • itsols
    itsols almost 2 years

    This is based on GCC/G++ and usually on Ubuntu.

    Here's my sample program I've done:

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        std::string c = "Test";
        cout << c;
        return 0;
    }
    

    The above code works fine.

    But I have two issues that I don't quite get...

    1. Writing the string declaration as std:string (with one :) also works fine. What's the difference?

    2. If I use std:string (with one :) within a class to declare a private variable, I get an error error: ‘std’ does not name a type. Example of this declaration:

    class KType
    {
    private:
        std:string N;
    };
    

    Can someone please explain these issues? Many thanks!

  • itsols
    itsols about 11 years
    @Matt-Petersson thanks for your inputs. I tried that but it was the same. And it DOES work in the first example. So I don't see why string in needed for the second :(
  • juanchopanza
    juanchopanza about 11 years
    @MikeSeymour no worries. I think it is all sorted now.
  • itsols
    itsols about 11 years
    That example of if I 'formatted it differently' is just beautiful. Thanks! Makes a lot of sense :)
  • itsols
    itsols about 11 years
    You said: namespace are imported into the global namespace. So why is this bad? Excuse me for my ignorance ;) And nice explanations +1
  • Andy Prowl
    Andy Prowl about 11 years
    @itsols: Watch out, the quote is slightly different that what your excerpt seems to imply ;) It's the names from the std namespace that get imported into the global namespace. The reason why this is bad is that you could get name clashes between names that belong to the global namespace and names that are imported into the global namespace.
  • itsols
    itsols about 11 years
    That wasn't a typo. Rather that's what I thought was correct as it worked before in the first example. But I see now that thre's a difference after Mike-Seymour's explanation. Thanks for taking the time!
  • itsols
    itsols about 11 years
    @juanchopanza Nice one: There is no difference, unless you declare something else called string. And then there was light :) +1