Header include problem - class not declared...

This is a discussion on Header include problem - class not declared... within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm new here, so I hope I'll ask my question correctly. I have an assignment to create a "text-based ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Header include problem - class not declared...

    Hello,
    I'm new here, so I hope I'll ask my question correctly. I have an assignment to create a "text-based cosmic adventure" and all went well until now. My project is separated into several header and implementation files. Problem arises only when I include certain file in another header file. Compiler writes the famous "myClass was not declared in this scope" error. And I really don't know what I'm doing wrong (or better said what to do).
    I have a tip, but better first some code:

    Code:
    // Location.h
    #include "MainMenu.h"
    class Location 
    {...}
    
    // MainMenu.h
    #include "Planet.h"
    class MainMenu
    {...}
    
    // Planet.h
    #include <vector>
    #include "Location.h"
    class Planet 
    {
    ...
    vector<Location> location; // error: 'Location' was not declared in this scope 
    ...
    }
    I think there could be some sort of cycle between Location.h and Planet.h. First Location.h includes MainMenu.h, which then includes Planet.h, that includes again Location.h. But I really don't know how to prevent this, because I need all headers to be included.

    Do you think this is the reason or is it something else? Could you please show me a solution to this problem?
    Thank you very much

    Note: When I do not include MainMenu.h in Location.h, the error in Planet.h doesn't occur. but I'd really like to be able to use MainManu functionality (well, better said, I need to, because it controls the whole game).


    P.S.: At the beginning of every file I of course have that thing against cycles:
    Code:
    #ifndef LOCATION_H
    #define LOCATION_H
    ...
    #endif
    so there shouldn't be any "inclusion cycle problem".

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's a bit hard to say without seeing actual code, but if "the error occurs when including MainMenu.h" that means with 99% certainty the error is in MainMenu.h. Does MainMenu.h require Location? Is it including the header file?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    One thing that strikes me: in your "examples", you consistently forget the terminating semi-colon for your class definitions. Now, this may just be carelessness with your pseudo-examples, but perhaps it manifests in your actual code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Yes, I have semi-collons in the real program.
    MainMenu.h doesn't require Location. It's including header file Planet.h.
    But MainMenu works well, there is no error.
    What do you think the problem could be?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then you'll have to provide more context. The bits you have (what little they are) should work as they stand.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Should it be std::vector<Location> instead of vector<Location>? Unless there is something in those headers that I'm not seeing that's opening up the std namespace.
    "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

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Yes, I have in one of this classes using namespace std.
    Meanwhile, I found a solution - I included MainMenu.h in Location.cpp and it works. But thanks for all your answers.

    But I afraid of how would I do this "include management" in larger projects. Are there any best practices in this area? How should one do it. I heard something about forward declarations, when they are useful?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    Forward declarations are useful when you don't need to know the real type. If you have a pointer or a reference to such a type, then a forward type is sufficient.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Ok, I think I got it. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem compiling
    By Programmer_P in forum C++ Programming
    Replies: 4
    Last Post: 03-01-2010, 02:43 PM
  2. Cannot declare inherited class of virtual Base
    By rogster001 in forum C++ Programming
    Replies: 6
    Last Post: 12-17-2009, 09:25 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

Tags for this Thread


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