Class not declared in the scope

This is a discussion on Class not declared in the scope within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm running into trouble here as I'm programming C++ with QT4.3. Here's a snippet Code: #Asteroid.h #ifndef ASTEROID_H #define ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    54

    Class not declared in the scope

    Hi,

    I'm running into trouble here as I'm programming C++ with QT4.3.

    Here's a snippet
    Code:
    #Asteroid.h
    #ifndef ASTEROID_H
    #define ASTEROID_H
    #include <QPainter>
    #include "AsteroidList.h"
    
    class Asteroid
    {
    
      public:
        Asteroid() { AsteroidList list; }
        void addAsteroid(int,int,int);
        void displayAsteroid();
        void draw(QPainter&);
    };
    #endif
    Code:
    #AsteroidList.h
    #ifdef ASTEROIDLIST_H
    #define ASTEROIDLIST_H
    
    #include <QPainter>
    #include "AsteroidNode.h"
    
    class AsteroidList
    {
       public:
         AsteroidList();
         Asteroid* first;
         void addAsteroid(int,int,int);
         void drawAsteroid(QPainter&);
         void display();
       private:
         AsteroidNode* a_first;
    };
    #endif
    Code:
    #AsteroidList.cpp
    #include "AsteroidList.h"
    #include <iostream>
    
    //Default Constructor
    AsteroidList::AsteroidList()
      {
        a_first = NULL;
      }
    As I compile with QT4.3, I encountered this error
    Asteroid.h: In constructor ‘Asteroid::Asteroid()’:
    Asteroid.h:15: error: ‘AsteroidList’ was not declared in this scope
    Asteroid.h:15: error: expected `;' before ‘list’
    Asteroid.cpp: In member function ‘void Asteroid::addAsteroid(int, int, int)’:
    Asteroid.cpp:13: error: ‘list’ was not declared in this scope
    Asteroid.cpp: In member function ‘void Asteroid::displayAsteroid()’:
    Asteroid.cpp:19: error: ‘list’ was not declared in this scope
    Asteroid.cpp: In member function ‘void Asteroid::draw(QPainter&)’:
    Asteroid.cpp:25: error: ‘list’ was not declared in this scope

    What i'm concerned is, why is the AsteroidList class not detected in Asteroid.h file even if I've included them?

    Please help.

    P.S. The codes are incomplete as I'm focusing only on why the class name is not there.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> #AsteroidList.h
    I hope that's not actually in your file. If you want to have the name of the file in the file, use:
    Code:
    // AsteroidList.h
    >> #ifdef ASTEROIDLIST_H
    That should be #ifndef, not #ifdef. I think this is causing your first error (and probably others).

    You are going to need a forward declaration for Asteroid in your AsteroidList.h file. Put this just below the includes:
    Code:
    class Asteroid;

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Daved View Post
    >>
    You are going to need a forward declaration for Asteroid in your AsteroidList.h file. Put this just below the includes:
    Code:
    class Asteroid;
    In addition to that you will have to change the constructor of Asteroid to take either a pointer or a reference to an AsteroidList.

    Quote Originally Posted by DarrenY
    What i'm concerned is, why is the AsteroidList class not detected in Asteroid.h file even if I've included them?
    That is because when including AsteoidList.h, AsteroidList.h includes Asteroid.h but then ASTEROIDLIST_H is already defined and AsteroidList.h will not be read anymore -> class AstreoidList is undefined for class Asteroid.

    Kurt

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is questionable, though, why an Asteroid would need to declare a local AsteroidList in its constructor. And if it is just a simplified example, still - does a single Asteroid need to know anything about the AsteroidList (and other Asteroids) or should it function on its own and let something else handle the interactions (e.g collisions)?
    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
    Jan 2005
    Posts
    7,328
    >> In addition to that you will have to change the constructor of Asteroid to take either a pointer or a reference to an AsteroidList.

    I don't think that's necessary. Asteroid can know about AsteroidList and include the definition of that class as long as AsteroidList only requires a forward declaration of Asteroid and doesn't need to include its full definition.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Daved View Post
    >> In addition to that you will have to change the constructor of Asteroid to take either a pointer or a reference to an AsteroidList.

    I don't think that's necessary. Asteroid can know about AsteroidList and include the definition of that class as long as AsteroidList only requires a forward declaration of Asteroid and doesn't need to include its full definition.
    Yes. Shure.
    My excuse is just that I read this
    Code:
        Asteroid() { AsteroidList list; }
    as a constructor that takes an AsteroidList as parameter (should have noticed that it makes little sense to pass a list by value ) and then got it all wrong.
    Next time I'll activate brains before posting.
    Kurt

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> I read this as a constructor that takes an AsteroidList as parameter

    Oops. I actually read it the same way both times I looked at it, too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 05-02-2009, 09:23 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  3. Replies: 2
    Last Post: 02-14-2008, 01:59 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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