I'm confused please help!

This is a discussion on I'm confused please help! within the C++ Programming forums, part of the General Programming Boards category; hi i'm a little confused about resolution operator (i think thats the name in english) Code: :: . i can ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    113

    I'm confused please help!

    hi i'm a little confused about resolution operator (i think thats the name in english)
    Code:
     ::
    .

    i can use the string class usng it that way:

    Code:
    #include <iostream>
    using namespace std;
    
    //can use any string class function without problem
    #include<string>
    using std::string;
    
    int main()
    {
         string my_name = "TerraKota";
        
    //need to know my_name size, so i use the size() method.
       cout << "You name is: " << my_name.size() << "chars long";
        
    //now if i need to store my name size into a variable i must
    //declare a size_type:
    
       size_type nameSize = my_name.size();
      
    //ok here the compiler generates an error:
    //C:\programas c++\ejer17_6\main.cpp(21) : error    C2065: 'size_type' : undeclared identifier//
      
    //but if i use :
        string:: size_type nameSize = my_name.size();
    //everything is fine.
        return 0;
    }
    so the question:
    if I include
    <string>
    and use the string class
    using std::string

    why i need to use string::size_type everytime i need to use the return value of the size() method?
    maybe im confused about namespaces etc, any help?

    and please excuse my poor english

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    because you only declared that you are using the std namespace for string not for anything thing else if you want everything to be in the std namespace then use using namespace std; and you won't need to std:: anything
    Woop?

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by terracota
    so the question:
    if I include
    <string>
    and use the string class
    using std::string

    why i need to use string::size_type everytime i need to use the return value of the size() method?
    maybe im confused about namespaces etc, any help?
    You won't need to do that if you also have using string::size_type; at the top of your program after the using std::string;.

    Having using std::string; simply means you don't have to put std:: in front of all your string declarations in your code, it does nothing to help you resolve the scope of what is in this case a typedef'd item within the string class.
    Last edited by hk_mp5kpdw; 08-24-2004 at 10:52 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    yes but im using std::string

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    I guess im not important enough to listen to. size_type does not have anything to do with string so its in its own seperate namespace so just put using std::size_type.
    Woop?

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>I guess im not important enough to listen to. size_type does not have anything to do with string so its in its own seperate namespace so just put using std::size_type.

    I don't think so...

    Here is part of the basic_string class (i cut out some parts):
    Code:
    class basic_string
    {
    private:
      struct Rep {
        size_t len, res, ref;
        bool selfish;
    
        charT* data () { return reinterpret_cast<charT *>(this + 1); }
        charT& operator[] (size_t s) { return data () [s]; }
        charT* grab () { if (selfish) return clone (); ++ref; return data (); }
    //...
        inline static void * operator new (size_t, size_t);
        inline static void operator delete (void *);
        inline static Rep* create (size_t);
        charT* clone ();
    
        inline void copy (size_t, const charT *, size_t);
        inline void move (size_t, const charT *, size_t);
        inline void set  (size_t, const charT,   size_t);
    
        inline static bool excess_slop (size_t, size_t);
        inline static size_t frob_size (size_t);
    
      private:
        Rep &operator= (const Rep &);
      };
    
    public:
    // types:
      typedef	   traits		traits_type;
      typedef typename traits::char_type	value_type;
      typedef	   Allocator		allocator_type;
      typedef size_t size_type;
    //...
    };
    As you can see, size_type is a typedef inside the std::basic_string class (and the std::string class is probably a typedef of the basic_string class)

    So you have to use scope resolution to specify that something is part of a class, structure or namespace
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    ok i understand that but if i include <string> the compiler "knows" wjeres defined size_type
    so i dont need to use string::size_type. or Im wrong?

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    size_type is defined in many different classes inside the standard library, so how would the compiler know that you want the string class's version? Yes, when you include <string>, the compiler sees one version of size_type, but it doesn't know that it is the one you want. It is the same concept as when you put using std::string. In that case, you are saying to the compiler, when I type "string", and you are trying to figure out what I meant, make sure to look in the std namespace for the definition. If you want to just use size_type, you must tell the compiler to look inside the string class for its definition.

    When you nest classes or typedefs or enums inside of classes, you are making a direct, logical link between them. For example, you can nest classes:
    Code:
    class A
    {
    public:
      class B
      {
      public:
        int a;
      };
    };
    
    int main()
    {
      int a;
      A::B myBInstance;
    }
    You can also nest enums:
    Code:
    class A
    {
    public:
      enum B
      {
        RED,
        GREEN,
        BLUE
      };
    };
    
    int main()
    {
      int a;
      A::B myBEnum = RED;
    }
    You can also nest typedefs:
    Code:
    class A
    {
    public:
      typedef int A_IntType;
    };
    
    int main()
    {
      int a = 3;
      A::A_IntType myInt = a;
    }
    So by adding the definition of the size_type typedef inside the basic_string class, the standard library designers are saying that when working with the basic_string class (or in your case, the string class), the data representing the size of the string has the type of size_type. You need the scope resolution operator because the size_type typedef is part of the string class, and you must distinguish between it and any other size_type definitions available.

    As hk_mp5kpdw said, you could put using string::size_type; so that the compiler always looks in the string class to find size_type when trying to find the appropriate definition. Like I said, it's the same concept as std::, just one level deeper.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    thanks for help
    and jlou... know im clear with your explanation, thanks a lot!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused
    By jeev2005 in forum C Programming
    Replies: 5
    Last Post: 06-01-2006, 02:04 PM
  2. Confused
    By (TNT) in forum C# Programming
    Replies: 1
    Last Post: 11-23-2005, 03:49 PM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 12:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 10:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21