Thread: Header include problem - class not declared...

  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,336
    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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,336
    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,817
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 03:43 PM
  2. Cannot declare inherited class of virtual Base
    By rogster001 in forum C++ Programming
    Replies: 6
    Last Post: 12-17-2009, 10: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, 10:33 AM

Tags for this Thread