bobobobo
bobobobo

Reputation: 67224

When is a forward declaration required?

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 refused to compile a class that had a pointer to ClassType, even though class ClassType is clearly defined in "ClassType.h".

Why isn't it 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;
};

Upvotes: 0

Views: 1085

Answers (8)

justadreamer
justadreamer

Reputation: 2440

in case it is a header guard of a kind

#ifdef SOMECLASS_H

then it is better to use #pragma once instead

Upvotes: 0

ralphtheninja
ralphtheninja

Reputation: 133008

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

Upvotes: 2

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506925

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.

Upvotes: 3

bluebrother
bluebrother

Reputation: 8886

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.

Upvotes: 0

Jim Buck
Jim Buck

Reputation: 20726

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.

Upvotes: 0

Fred Larson
Fred Larson

Reputation: 62063

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

Upvotes: 3

J. Polfer
J. Polfer

Reputation: 12481

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

Upvotes: 0

RichieHindle
RichieHindle

Reputation: 281455

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

Upvotes: 7

Related Questions