Namespaces and declarations

Discussion in 'C++' started by kk565, Jan 27, 2008.

  1. kk565

    kk565 Guest

    Apologies if this has been asked already, but my searches have turned up
    nothing.

    I have a class declaration inside a header file similar to this:

    struct MyClass
    {
    class mytype_a *m1;
    class mynamespace1::mynamespace2::mytype_b *m2;
    };

    Whereas just mytype_a compiles just fine, at the second member
    declaration the compiler (GCC 4) gives the error:
    ‘mynamespace1’ has not been declared

    My assumption was that since this is a just a pointer declaration, the
    compiler would not need to know the actual details of the type (at least
    at this stage)

    To fix this, does it mean that I have to include the header file
    containing the mytype_b type (and all namespace declarations)? or is
    there a way of just letting the compiler know about these namespaces
    without including the actual declarations?

    Also my first instinct was to use something like this
    struct MyClass
    {
    using namespace mynamespace1;
    };

    however this appears not be valid C++. Is there anyway of declaring used
    namespaces or components from namespaces within a class
    declaration/implementation?

    Thanks in advance.
    kk565, Jan 27, 2008
    #1
    1. Advertising

  2. * kk565:
    > Apologies if this has been asked already, but my searches have turned up
    > nothing.
    >
    > I have a class declaration inside a header file similar to this:
    >
    > struct MyClass
    > {
    > class mytype_a *m1;
    > class mynamespace1::mynamespace2::mytype_b *m2;
    > };
    >
    > Whereas just mytype_a compiles just fine, at the second member
    > declaration the compiler (GCC 4) gives the error:
    > ‘mynamespace1’ has not been declared
    >
    > My assumption was that since this is a just a pointer declaration, the
    > compiler would not need to know the actual details of the type (at least
    > at this stage)
    >
    > To fix this, does it mean that I have to include the header file
    > containing the mytype_b type (and all namespace declarations)? or is
    > there a way of just letting the compiler know about these namespaces
    > without including the actual declarations?


    The following compiles fine with three compilers:


    class mytype_a;

    namespace mynamespace1
    {
    namespace mynamespace2
    {
    class mytype_b;
    }
    }

    struct MyClass
    {
    class mytype_a *m1;
    class mynamespace1::mynamespace2::mytype_b *m2;
    };

    int main() {}


    However, the "class" keyword is superfluous, write just


    struct MyClass
    {
    mytype_a *m1;
    mynamespace1::mynamespace2::mytype_b *m2;
    };


    By the way, since that indicates that you're new to C++: it's generally
    not a good idea to use raw pointers directly. Depending on what you're
    using them for consider library classes such as std::vector or smart
    pointers such as boost::shared_ptr. That takes care of a lot of
    problems, including the ambiguity about what the pointers are used for.


    > Also my first instinct was to use something like this
    > struct MyClass
    > {
    > using namespace mynamespace1;
    > };
    >
    > however this appears not be valid C++. Is there anyway of declaring used
    > namespaces or components from namespaces within a class
    > declaration/implementation?


    No, but you can (for example) define a namespace alias outside of the class:


    namespace blah = mynamespace1::mynamespace2;

    struct MyClass
    {
    mytype_a *m1;
    blah::mytype_b *m2;
    };


    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jan 27, 2008
    #2
    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. matthew polder

    namespaces and forward declarations

    matthew polder, Jul 24, 2003, in forum: C++
    Replies:
    1
    Views:
    353
    John Harrison
    Jul 24, 2003
  2. whithers
    Replies:
    4
    Views:
    372
    Sumit Rajan
    Jan 16, 2004
  3. Steven T. Hatton

    forward declarations and namespaces?

    Steven T. Hatton, Apr 27, 2004, in forum: C++
    Replies:
    6
    Views:
    3,226
    Dave Moore
    May 5, 2004
  4. Replies:
    1
    Views:
    278
    Matthew Crisanti
    Aug 4, 2007
  5. Victor Bazarov

    Re: Forward declarations and namespaces

    Victor Bazarov, Mar 20, 2009, in forum: C++
    Replies:
    5
    Views:
    1,148
    Victor Bazarov
    Mar 23, 2009
Loading...

Share This Page