why forward declearation

This is a discussion on why forward declearation within the C++ Programming forums, part of the General Programming Boards category; I've a file called var.h and I've class Var n it. when I am doing #include "var.h" Its not throwing ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    why forward declearation

    I've a file called var.h and I've class Var n it.

    when I am doing #include "var.h"
    Its not throwing any compilation errors.
    but saying Var undecleared.

    If I just do a forward Declearation without #include Directive Its also firing some other errors.

    However when I do both #include and forward declaration it compiles.

    No my question is why !!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Can you post the code? Your explanation is confusing. You said "Its not throwing any compilation errors" but then said, "but saying Var undeclared." Isn't that a compilation error?

    What file are you editing? Is it another header or a source file?

    Posting the code would answer a lot of these questions.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Quote Originally Posted by Daved View Post
    Can you post the code? Your explanation is confusing. You said "Its not throwing any compilation errors" but then said, "but saying Var undeclared." Isn't that a compilation error?

    What file are you editing? Is it another header or a source file?

    Posting the code would answer a lot of these questions.
    my words might be confusing.

    in paramsmap.h If I keep both #include and forward declaration of var it compiles but If I Just use either #include "var.h" or forward declaration of Var class it doesn't compile.

    and here is my code.

    paramsmap.h
    Code:
    #ifndef PARAMSMAP_H
    #define PARAMSMAP_H
    
    #include <string>
    #include <map>
    #include "var.h"
    #include "paramsmapcore.h"
    
    class Var;
    
    using std::string;
    using std::map;
    
    /**
    	@author Neel Basu <neel@zigmoyd>
    */
    class ParamsMap : public ParamsMapCore<string, Var>{
    	public:
        ParamsMap();
        ~ParamsMap();
    };
    
    #endif
    paramsmapcore.h
    Code:
    #ifndef PARAMSMAPCORE_H
    #define PARAMSMAPCORE_H
    
    #include <map>
    #include <string>
    //This class doesn't need the var class.
    
    /**
    	@author Neel Basu <neel@zigmoyd>
    */
    template <typename KeyType, typename ValueType>
    class ParamsMapCore : public std::map<KeyType, ValueType>{
      public:
        ParamsMapCore();
        ~ParamsMapCore();
      public:
        .....
    };
    #include "paramsmapcore.hpp"
    #endif
    Last edited by noobcpp; 10-23-2008 at 09:01 AM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    No way to tell from that. What are the errors exactly? Basically it seems that you are quite randomly including and forward declaring things without knowing what it means. (May-be you would be able to make the compiler output preprocessed source, so that you can see what the compiler is seeing after all the includes.)

    As a side note, I downloaded this project yesterday and noticed that you have declared some things in a namespace only to bring these names into global namespace in a different header file. Better not use using declarations in headers as it practically defeats the purpose of namespaces.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    It says error: invalid use of incomplete type 'struct Var' and more similer errors.

    Do you think that where there is a case of cyclic inclusion I've used #include instead of forward declaration. etc.. things ??

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    it sounds like you didn't include var.h in ParamsMap.cpp

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Quote Originally Posted by m37h0d View Post
    it sounds like you didn't include var.h in ParamsMap.cpp
    did you mean in paramsmap.h ??

    as you can see above code its included.

    but the thing is only #include is not enough its saying undeclared Var or similar If forward declaration is not there.

    and If I use only #include its also showing other errors too.

    I am in a full PANIC STATE.

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    get rid of #include "paramsmapcore.hpp" at the bottom of that file...
    from what i can tell, you have an infinite recursive definition for that header file, because the #endif doesn't close off the #ifndef, so it's not defined yet, but you include the same file.

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    But #endif should close #ifndef because paramsmapcore.hpp is never included directly from any other file.
    only from pparamsmapcore.h so if paramsmap.h is included once the hpp is also included once..

  10. #10
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Thanks

    I solved the problem however the problem was not only in these files.
    But in a lot other files too.

    To solve this Problem I took a Paper and pen to draw inclusion diagram by hand and then realized one A4 page is not enough and then took 4 pages and it was too much pain. I never drew such a Big diagram MANUALLY on paper LOL.

    latter I realized that there was UNNECESSARY cyclic inclusion. and forward declarations once I cleared those it compiled nicely.

    I was suffering for this problem for more than 7 days.

  11. #11
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839

    Thumbs up

    Quote Originally Posted by noobcpp View Post
    did you mean in paramsmap.h ??

    as you can see above code its included.

    but the thing is only #include is not enough its saying undeclared Var or similar If forward declaration is not there.

    and If I use only #include its also showing other errors too.

    I am in a full PANIC STATE.
    no, i didn't. the forward declaration pattern i typically use is like so:

    Code:
    //foo.h
    #ifndef FOOH
    #define FOOH
    class bar;
    class foo
    {
        bar *b;
        void someMethod();
        foo();
        foo(bar *_b);
    };
    #endif
    Code:
    //foo.cpp
    #include "bar.h"
    #include "foo.h"
    
    foo::foo():
        b(NULL)
    {
    }
    
    foo::foo(bar *_b):        
        b(_b)
    {
    }
    
    void foo::someMethod()
    {
        if(b!=NULL)
        {
            b->somePropertyOfB;
            //...
        }
        else
        {
            //do something else  
        }
            //etc.    
    };
    this works because a pointer is always the same size, so the contents of bar are unimportant, however classes have an unknown size until their body is defined. this can be done in the cpp file without worrying about cyclic inclusions because you always include the header files, not the implementation files.

    however, i also didn't see at the time that you were instantiating ParamsMap as a descendant of a template class using Var (as opposed to var* or var&). that seems very suspicious to me that you would be able to do that without the class being defined.

    i've never tried to do it the way you are there, but AFAIK there is no way to use forward declarations and then refer to the type by value before its body has been defined.

    my guess is you aren't actually using forward declaration at all here, but again, i haven't tried deriving from a template like that, so i could be wrong. if so, i'm sure someone will correct me there.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I have not read any of the replies or even the intial post.

    Forward declaration resolves needing to have a huge unreadable C/C++ file. Instead you can break a program down into smaller more managable files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does gcc hate Forward declarations?
    By SevenThunders in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2009, 02:03 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  3. forward declaration
    By C_ntua in forum C++ Programming
    Replies: 14
    Last Post: 09-29-2008, 11:29 AM
  4. circular dependencies, forward doesn't help
    By pheres in forum C++ Programming
    Replies: 14
    Last Post: 11-24-2006, 03:15 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

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