views:

273

answers:

8

I have this C++ project that I'm working on

All classes have their implementation separated from the .h file.

However, I'm not certain why/when forward declarations are required.

For example, I just ran into an error when I #included "ClassType.h", the compiler completely refuses to compile a class that had a pointer to ClassType, even though class ClassType is clearly defined in "ClassType.h".

Why it isn't enough for the compiler to simply see that I've #included "ClassType.h", and WHY does it want a forward declaration?

#include "ClassType.h"

// REFUSES TO COMPILE WITHOUT forward declaration
class ClassType;

class SomeClass
{
    ClassType* instance;
};
+5  A: 

Does ClassType.h include (directly or indirectly) the file you quoted, in which you're defining SomeClass?

RichieHindle
A: 

The problem probably exists with ClassType, and not SomeClass. Please post the listing for ClassType.h, and I believe the community will be able to help you. :)

sheepsimulator
+2  A: 

I'm wondering if ClassType is declared in a namespace. Need to see ClassType.h to be sure.

Fred Larson
A: 

If you are using a forward declaration in order to get the compile to work, then ClassType.h has something that is making your ClassType definition not make into the pre-processed source that gets compiled. Try compiling so that it leave the pre-processed file around and see what you can find there.

Jim Buck
A: 

In general, you need a forward declaration in any compile unit if the compiler hasn't seen the declaration he's trying to use. This means you need forward declarations

  • if you have cyclic dependencies
  • if the header doesn't properly declare the class (function, variable) you're trying to use.

Including a header file does not do any declarations -- it's just including a file, and that file could be very well empty (or made empty due to broken double inclusion protection, which can happen easily if you copy the header file from another one and forget to adjust the macro used for double inclusion protection).

In your case, I'd bet something is wrong with the ClassType.h file. If it does declares the class my guess is the double inclusion protection.

bluebrother
+2  A: 

Sounds like you have the same header guard. Check that at the start of your header, you don't have something like this

#ifndef SOMECLASS_H

in both files. Those guards have to have unique names in every header file they guard from multiple inclusions. Otherwise, they will prevent inclusion of content into other headers.

Johannes Schaub - litb
+2  A: 

When is a forward declaration required?

It is needed when you only need to declare a pointer to a type, i.e. in a header file. The actual implementation is completely irrelevant and should only be needed in the .cpp files using any method or state of that class.

As long as a pointer is being copied, the type is irrelevant as you are merely just copying a pointer value, not the content of what the pointer points to.

// someclass.h
class ClassType;

class SomeClass
{
  public:
    void foo();
    ClassType* instance;
};

// someclass.cpp
void SomeClass::foo()
{
    instance->SomeMethod();  // someclass.h needed
    SomeFunction(instance);  // someclass.h irrelevant
}

Cheers

If this doesn't work, you probably have some problems with someclass.h

Magnus Skog
A: 

in case it is a header guard of a kind

#ifdef SOMECLASS_H

then it is better to use #pragma once instead

justadreamer