CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2002
    Posts
    309

    How to - forward declare a nested class or all within namespace?

    How to - forward declare a nested class or all also within namespace, -
    If there is inclusion of declared class to declared earlier in other file ? So - impossible to make a include?
    Thanks.

    (double side reference) ??
    Like:

    file1.h ----
    #include "file2.h"
    namespace 1_2
    {

    class 1
    {
    public:
    host* member_host;
    class 2{};
    };

    }

    file2.h---

    //impossible to include file1.h !!! -only forward declarations !
    class host
    {
    class2* member; //Errors - 'class2 -undefined'
    };

    };#include "file2.h" #include "file2.h"

  2. #2
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Since member is a class2 *, just forward declare class2.
    Code:
    //A.h
    #include "B.h"
    class A
    {
    B *b;
    };
    
    //B.h
    class A;
    class B
    {
    A *a;
    };
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  3. #3
    Join Date
    Aug 2002
    Posts
    309

    this not works with multiple type -inclusions

    this not works with type -inclusions
    class - within class-within namespace.

    In my sample how you can resolve that forward definition

    class namespace 1_2::class1::class2;

    generates "use of undefined class1" Error.??
    Last edited by vgrigor3; September 10th, 2002 at 08:17 AM.

  4. #4
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    That's because class1 isnt't in the global namespace, but in namespace 1_2 (wich, btw, isn't a legal identifier). You have to resolve the scope, by prefixing the declaration with the name of the namespace.

    Code:
    class host;
    namespace N{
    	class A
    	{
    	public:
    		host *host_;
    		class B{};
    	};
    }
    
    class host{
    	N::A::B *b_;
    };
    Last edited by Gabriel Fleseriu; September 10th, 2002 at 08:19 AM.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  5. #5
    Join Date
    Aug 2002
    Posts
    309
    I had used full qualifying name, as changed in sample above,
    but multiple type-inclusion not works,

    than please,
    can you chang you sample code to show how it must be right at
    your opinion with all -namespace and nested class?

  6. #6
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Code:
    //file1.h
    class host;
    namespace N
    {
    	class A
    	{
    	public:
    	host* host_;
    	class B{};
    	};
    }
    
    //file2.h
    #include "file1.h"
    class host
    {
    	N::A::B* member;
    };
    
    // ???.cpp file
    #include "file2.h"
    //.....
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  7. #7
    Join Date
    Aug 2002
    Posts
    309

    no any forward declaration at your sample ?

    Sorry, - important -
    file2 must be included to class 1, by conditions


    at that forward declaration of?
    of N::A::B
    must be..

    How it can be?
    Last edited by vgrigor3; September 10th, 2002 at 08:42 AM.

  8. #8
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    I don't see how that could work. Maybe you can rethink your design.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

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