simple namespace question

Discussion in 'C++' started by michael, Apr 18, 2007.

  1. michael

    michael Guest

    Hi all,

    why if I can use

    std::vector<int>::iterator variable;

    can I not use

    using std::vector<int>::iterator;

    to save typing the std::vector<int>:: each time I use it?
    I have a text which has the 'using' statement in it, but when I compile I
    get errors like "std::vector<int, std::allocator<int>> is not a namespace".
    I don't understand why, if someone could explain it I would be gratefull.

    Thanks for your help

    Regards

    Michael
     
    michael, Apr 18, 2007
    #1
    1. Advertising

  2. michael wrote:
    > Hi all,
    >
    > why if I can use
    >
    > std::vector<int>::iterator variable;
    >
    > can I not use
    >
    > using std::vector<int>::iterator;


    That's illegal. This is legal.

    typedef std::vector<int>::iterator iterator;
     
    Gianni Mariani, Apr 18, 2007
    #2
    1. Advertising

  3. michael

    Ian Collins Guest

    michael wrote:
    > Hi all,
    >
    > why if I can use
    >
    > std::vector<int>::iterator variable;
    >
    > can I not use
    >
    > using std::vector<int>::iterator;
    >
    > to save typing the std::vector<int>:: each time I use it?
    > I have a text which has the 'using' statement in it, but when I compile I
    > get errors like "std::vector<int, std::allocator<int>> is not a namespace".
    > I don't understand why, if someone could explain it I would be gratefull.
    >

    The error says it all. You can only use a using directive with a
    namespace. Use a typedef instead.

    --
    Ian Collins.
     
    Ian Collins, Apr 18, 2007
    #3
  4. michael

    michael Guest

    "Ian Collins" <> wrote in message
    news:...
    > michael wrote:
    >> Hi all,
    >>
    >> why if I can use
    >>
    >> std::vector<int>::iterator variable;
    >>
    >> can I not use
    >>
    >> using std::vector<int>::iterator;
    >>
    >> to save typing the std::vector<int>:: each time I use it?
    >> I have a text which has the 'using' statement in it, but when I compile I
    >> get errors like "std::vector<int, std::allocator<int>> is not a
    >> namespace".
    >> I don't understand why, if someone could explain it I would be gratefull.
    >>

    > The error says it all. You can only use a using directive with a
    > namespace. Use a typedef instead.
    >
    > --
    > Ian Collins.


    Thanks for the help Ian,

    I'm still a bit confused though. What exactly makes
    std::vector<int>::iterator not a namespace when for example std::endl is?
    What is it that determines whether something is a namespace or not?

    thanks for the help

    regards

    Michael
     
    michael, Apr 19, 2007
    #4
  5. michael

    Ian Collins Guest

    michael wrote:
    > "Ian Collins" <> wrote in message
    > news:...
    >
    >>michael wrote:
    >>
    >>>Hi all,
    >>>
    >>>why if I can use
    >>>
    >>>std::vector<int>::iterator variable;
    >>>
    >>>can I not use
    >>>
    >>>using std::vector<int>::iterator;
    >>>
    >>>to save typing the std::vector<int>:: each time I use it?
    >>>I have a text which has the 'using' statement in it, but when I compile I
    >>>get errors like "std::vector<int, std::allocator<int>> is not a
    >>>namespace".
    >>>I don't understand why, if someone could explain it I would be gratefull.
    >>>

    >>
    >>The error says it all. You can only use a using directive with a
    >>namespace. Use a typedef instead.
    >>

    *Please* don't quote signatures.
    >
    > Thanks for the help Ian,
    >
    > I'm still a bit confused though. What exactly makes
    > std::vector<int>::iterator not a namespace when for example std::endl is?
    > What is it that determines whether something is a namespace or not?
    >

    Using std::vector or using std::cout are legal. Using only works with a
    namespace, or an object or value from a namespace, not a type from a
    class in a namespace. std::vector<int>::iterator goes one step too far.
    Use a typedef.

    --
    Ian Collins.
     
    Ian Collins, Apr 19, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    599
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,167
  3. Anonymous
    Replies:
    3
    Views:
    561
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    469
    Woebegone
    Nov 19, 2004
  5. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,435
    Howard
    Nov 29, 2004
Loading...

Share This Page