Declaration problems

This is a discussion on Declaration problems within the C++ Programming forums, part of the General Programming Boards category; Hi, This is giving me some headache: Code: //FILE: AREA.H #ifndef AREA_H #define AREA_H #include <QObject> #include "camera.h" class Area ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    Declaration problems

    Hi,

    This is giving me some headache:

    Code:
    //FILE: AREA.H
    
    #ifndef AREA_H
    #define AREA_H
    
    #include <QObject>
    
    #include "camera.h"
    
    class Area
    {
    public:
        Area(QString text);
        ~Area();
    
    	static void Dispose();
    	
        static QList<Area*> *areas;
        
        QList<Camera*> *cameras;
        QString       text;
    private:
        QString name;
    };
    
    #endif
    
    //FILE:CAMERA.H
    
    #ifndef CAMERA_H
    #define CAMERA_H
    
    #include <QObject>
    
    #include "area.h"
    
    class Camera
    {
    public:
        Camera(QString text, Area* parentArea);
        ~Camera();
    
    	QString text;
    private:
    	Area* parentArea;
    	QString name;
    };
    
    #endif
    Compiler output: "src/camera.h:14: error: 'Area' was not declared in this scope"

    I think the issue is on the fact that area.h is including camera.h and vice-versa, so i tried to add "class Camera;" in area.h and "class Area;" in camera.h and then it gave the following output:

    "src/....../listwidgethandler.h:20: error: 'Area' is not a type"
    "src/....../listwidgethandler.h:21: error: 'Camera' is not a type"

    Ideas??

    obs: the classes with Q in the beggining aren't the problem.

    Compiler: GCC 4.1
    OS: Linux Kubuntu Feisty Fawn (7.03)
    IDE: QDevelop
    Last edited by Scarvenger; 05-06-2007 at 04:07 PM.

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    88
    I'm not positive, but I think you may be able to solve this problem with forward declarations, like this:

    Code:
    //FILE: AREA.H
    
    #ifndef AREA_H
    #define AREA_H
    
    #include <QObject>
    
    //Instead of including camera.h, do a forward declaration
    class Camera;
    
    class Area
    {
    public:
        Area(QString text);
        ~Area();
    
    	static void Dispose();
    	
        static QList<Area*> *areas;
        
        QList<Camera*> *cameras;
        QString       text;
    private:
        QString name;
    };
    Do the same for camera.h (class Area; instead of #include area.h), and then in your .cpp implementations of each class, include the appropriate headers.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    still don't work:

    Code:
    #ifndef CAMERA_H
    #define CAMERA_H
    
    #include <QObject>
    
    class Area;
    
    class Camera
    {
    public:
        Camera(QString text, Area* parentArea);
        ~Camera();
    
    	QString text;
    private:
    	Area* parentArea;
    	QString name;
    };
    
    #endif
    Output: "'Area' is not a type"

    Im thougth in one way to solve: putting the two classes declarations in the same header file, but wouldn't it be like "bad programming"?

  4. #4
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Even with my strange idea of joining two classes in one header went wrong.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    > Output: "'Area' is not a type"

    What line is the error on? It seems as though this should work (more info: here)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    line 14 at camera.h file, and i will see the link thanks.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    I have read the text in the link but everything that is there i already tried.

    Ideas?

  8. #8
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Ok, i found what was wrong: i had the following piece of code:
    Code:
    enum Types
    {
        Camera,
        Area
    };
    So frustrating [:P]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Class declaration problems
    By scwizzo in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2008, 06:47 PM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  4. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  5. Class Declaration?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 07-27-2002, 09:09 PM

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