3

我正在尝试为我的研究实现有限元代码。我需要创建一个将材质与其名称相关联的地图,以便我可以按名称访问它们。从主类的头文件中,我定义了以下内容:

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

主类的实现编译。但是,当 makefile 到达主文件时,我收到以下错误

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;

我该如何解决这个问题。我的代码的所有其他文件都包含重要代码类的头文件正在编译。

谢谢!

4

2 回答 2

4

该标准要求用于在库中实例化模板的类型在实例化时是完整的,但记录了一些例外情况(例如std::shared_ptr<T>)。如果不提供类型的完整定义,您将无法修复Material代码。也就是说:您不能使用刚刚声明的类型来实例化 stl 容器。

于 2013-06-24T22:39:33.823 回答
2

您必须定义完整的类。但是,您可以将 aMaterial*放入容器中,或者如果这还不够好,您可以将 astd::unique_ptr<Material>放入容器中,这样内存管理将为您处理,但仍然允许您向​​前声明通往幸福的道路。

于 2013-06-24T22:45:30.607 回答