CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: string

  1. #1
    Join Date
    Nov 2004
    Posts
    13

    string

    Hi, I'm having some trouble using the string data type I'm very new to c++:

    I would like to have a class with a non sataic data member of type string. as defined in #include <string>

    Here is the jist of what I'm trying to do:


    #include <string>
    using namespace std;

    class treeNode
    {
    public:
    //lots of methods
    private:
    string nodeElement = "text";// a string that is in each node of a tree

    }

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: string

    You can't initiliaze your string in your .H file. Do something like:
    .h
    Code:
    #include <string>
    using namespace std;
    
    class treeNode
    {
    public:
        //lots of methods
    private:
        string nodeElement;
    }
    .cpp
    Code:
    treeNode::treeNode() : nodeElement("text")
    {
    ...
    }
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: string

    Quote Originally Posted by hexaplus
    #include <string>
    using namespace std;
    As a small side note...it is not a good idea to map the complete namespace 'std' to your application in a header file (with 'using namespace std'). Every file that includes this header will get everything out of the namespace as well which is not desirable...

    The following shows you the four different methods to map a namespace...
    Code:
    // Using the full member name, including the namespace it belongs to:
    std::cout << "Hello World";
    
    
    // By taking advantage of Using-Declarations:
    using std::cout;                             // This declares cout in the current
                                                 // scope as synonym for std::cout
    cout << "Hello World";
    
    
    // By taking advantage of Using-Directives:
    using namespace std;                         // Which specifies that the current
                                                 // scope can refer to names in the
                                                 // 'std' namespace without using
                                                 // full qualifiers. This is mostly
                                                 // used when porting legacy code.
    cout << "Hello World";
    
    
    // Using aliases:
    namespace X
    {
      namespace Y
      {
        class Z { ... };
      }
    }
    
    X::Y::Z                                      // The full qualifier for 'Z' is
                                                 // 'X::Y::Z'
    
    namespace w = X::Y;                          // This declares 'w' as an alias for
                                                 // namespace 'X::Y'
    
    w::Z                                         // Access 'Z' using 'w::Z'

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: string

    Quote Originally Posted by Andreas Masur
    As a small side note...it is not a good idea to map the complete namespace 'std' to your application in a header file (with 'using namespace std'). Every file that includes this header will get everything out of the namespace as well which is not desirable...
    You are absolutely correct. I just copied/paste the code and didn't pay attention to that using namespace
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Nov 2004
    Posts
    13

    sweet

    thanks

  6. #6
    Join Date
    Nov 2004
    Posts
    13

    like here?

    so something like this in the stead:

    using std::string;

  7. #7
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: string

    Nope.. Not quite. That will also pollute the global namespace. In your header, specify the std namespace explicitly on every string you declare:
    Code:
    std::string some_string;
    In your cpp file however, you can bring in as many namespaces as you like since it will only pollute the global namespace of that particular file. (And not all files that include it like it would in a header.)
    Insert entertaining phrase here

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: string

    Quote Originally Posted by wien
    Nope.. Not quite. That will also pollute the global namespace.
    Well...actually, it will only declare 'string' in the current scope as a synomyn for 'std::string'...in other words, it does not map the complete namespace to your application...

  9. #9
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: string

    Quote Originally Posted by Andreas Masur
    Well...actually, it will only declare 'string' in the current scope as a synomyn for 'std::string'...in other words, it does not map the complete namespace to your application...
    Well, you do bring that "string" name in there.. It's not as bad as putting "using namespace" in the global scope, no, but should still be avoided.

    However, if you put the "using" line inside the scope of an inline function, that is, not in the global scope, it's a completely different matter.
    Insert entertaining phrase here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured