3

I am trying to implement a finite element code for my research. I need to create a map that associates materials with their names so that I can access them by name. From the header file of the main class, I define the following:

/// Friend class definition for the materials
friend class Material; 
/// Class definition for the materials
class Material; 

/// Creates a dictionary of elementsets vs material names
std::map<std::string,std::string>  _material_assignment; 

/// Container to hold the material names of all the materials
std::map<std::string,Material> _materials; 

The implementation of the main class compiles. However, when the makefile reaches the main file, I get the following error

Error: 'std::pair<_T1, _T2>::second' has incomplete type
       _T2 second;                /// @c second is a copy of the second object
       ^
In file included from src/main.C:5:0:
src/diff_code.h:228:9: error: forward declaration of 'class DiffCode::Material'
   class Material;

How do I get around this. All the other files of my code which all include the header file for the important code class are compiling.

Thanks!

  • 1
    What's preventing you from including the definition of the `Material` class instead of only the forward declaration? – Praetorian Jun 24 '13 at 22:33

2 Answers2

4

The standard requires that the types used to instantiate the templates in the library are complete at the point of instantiations with a few exceptions documented (for example std::shared_ptr<T>). You cannot fix the code without providing the full definition of the Material type. That is: you cannot instantiate an stl container with a type that has just been declared.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
2

You have to have the full class defined. However, you can put a Material* in the container, or if that's not good enough, you can put a std::unique_ptr<Material> in the container, so the memory management would be handled for you but still allow you to forward-declare your way to happiness.

Jamin Grey
  • 10,151
  • 6
  • 39
  • 52