c++ - 为刚刚声明的类定义 STL 容器。

标签 c++ stl friend-class

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

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

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

谢谢!

最佳答案

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

关于c++ - 为刚刚声明的类定义 STL 容器。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17286201/

相关文章:

c++ - error : expected constructor, destructor,或者 ‘;’ token CEGUI之前的类型转换

C++ 将 double 中的每个小数转换为数组元素?

c++ - 有移动范围的算法吗?

c++ - 为什么我不能在 friend 类中实例化一个构造函数是私有(private)的类?

c++ - friend 类和访问者部分的定义

c++ - 使用 SetTimer 传递用户数据

c++ - const 和非常量函数的重载如何工作?

c++ - STL中构造函数调用的顺序

c++ - 复制和移动构造函数是自动的 friend 吗?

c++ - 为什么 `cin >> noskipws` 不等待输入?