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 ...

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.

>>
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.

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.

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)?

>> 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.

>> 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