Question

If I create a class like so:

// B.h
#ifndef _B_H_
#define _B_H_

class B
{
private:
    int x;
    int y;
};

#endif // _B_H_

and use it like this:

// main.cpp
#include <iostream>
#include <vector>

class B; // Forward declaration.

class A
{
public:
    A() {
        std::cout << v.size() << std::endl;
    }

private:
    std::vector<B> v;
};

int main()
{
    A a;
}

The compiler fails when compiling main.cpp. Now the solution I know is to #include "B.h", but I'm curious as to why it fails. Neither g++ or cl's error messages were very enlightening in this matter.

Was it helpful?

Solution

The compiler needs to know how big "B" is before it can generate the appropriate layout information. If instead, you said std::vector<B*>, then the compiler wouldn't need to know how big B is because it knows how big a pointer is.

OTHER TIPS

In fact your example would build if A's constructor were implemented in a compile unit that knows the type of B.

An std::vector instance has a fixed size, no matter what T is, since it contains, as others said before, only a pointer to T. But the vector's constructor depends on the concrete type. Your example doesn't compile because A() tries to call the vector's ctor, which can't be generated without knowing B. Here's what would work:

A's declaration:

// A.h
#include <vector>

class B; // Forward declaration.

class A
{
public:
    A(); // only declare, don't implement here

private:
    std::vector<B> v;
};

A's implementation:

// A.cpp
#include "A.h"
#include "B.h"

A::A() // this implicitly calls vector<B>'s constructor
{
    std::cout << v.size() << std::endl;
}

Now a user of A needs to know only A, not B:

// main.cpp
#include "A.h"

int main()
{
    A a; // compiles OK
}

To instantiate A::v, the compiler needs to know the concrete type of B.

If you're trying to minimize the amount of #included baggage to improve compile times, there are two things you can do, which are really variations of each other:

  1. Use a pointer to B
  2. Use a lightweight proxy to B

It's more than just the size of B that's needed. Modern compilers will have fancy tricks to speed up vector copies using memcpy where possible, for instance. This is commonly achieved by partially specializing on the POD-ness of the element type. You can't tell if B is a POD from a forward declaration.

Just like fyzix said, the reason your forward declaration is not working is because of your inline constructor. Even an empty constructor might contain lots of code, like the construction of non-POD members. In your case, you have a vector to initialize, which you can't do without defining its template type completely.

The same goes for destructors. The vector needs the template type definition to tell what destructor to call when destroying the instances it holds.

To get rid of this problem, just don't inline constructors and destructors. Define them separately somewhere after B is completely defined.

For more information, http://www.chromium.org/developers/coding-style/cpp-dos-and-donts

This doesn't matter whether you use a vector or just try to instantiate one B. Instantiation requires the full definition of an object.

Man, you're instancing std::vector with an incomplete type. Don't touch the forward declaration, just move the constructor's definition to the .cpp file.

The reason you can't use a forward declaration is because the size of B is unknown.

There's no reason in your example that you can't include B.h inside of A.h, so what problem are you really trying to solve?

Edit: There's another way to solve this problem, too: stop using C/C++! It's so 1970s... ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top