• VC++ 在两个文件互相包含时会出现的错误


    首先,要分别在两个文件中实现以下两个类

     

    class Object

    {

    public:

      NewType ToType();

    };

     

     

    class NewType : public Object

    {

    }

     

     

         --------------------------------------------------------------------------------    做法1  ---------------------------------------------------------

     

    //在文件Object.h 中定义

    #include "NewType.h"

    class Object

    {

    public:

      NewType ToType();

    };

     

     

    //在文件NewType.h 中定义

    #include "Object.h"

    class NewType : public Object

    {

    }

     

    将产生错误:

    "warning C4182: #include nesting level is 363 deep; possible infinite recursion"

    "fatal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit"

    原因是两个文件互相包含,导致包含的层次太深

     

     

         --------------------------------------------------------------------------------    做法2  ---------------------------------------------------------

     

    //在文件Object.h 中定义

    #include "NewType.h"

     

    #ifndef _OBJECT_H

    #define _OBJECT_H

     

    class Object

    {

    public:

      NewType ToType();

    };

     

    #endif

     

     

    //在文件NewType.h 中定义

    #include "Object.h"

     

    #ifndef _NEWTYPE_H

    #define _NEWTYPE_H

     

    class NewType : public Object

    {

    }

     

    #endif

     

    错误依旧

     

     

         --------------------------------------------------------------------------------    做法3  ---------------------------------------------------------

     

    //在文件Object.h 中定义

    #ifndef _OBJECT_H

    #define _OBJECT_H

     

    #include "NewType.h"

     

    class Object

    {

    public:

      NewType ToType();

    };

     

    #endif

     

     

    //在文件NewType.h 中定义

    #include "Object.h"

     

    #ifndef _NEWTYPE_H

    #define _NEWTYPE_H

     

    class NewType : public Object

    {

    }

     

    #endif

     

    产生错误:

    "error C2504: 'Object' : base class undefined"

      

     

         --------------------------------------------------------------------------------    做法4  ---------------------------------------------------------

     

    //在文件Object.h 中定义

    #include "NewType.h"

     

    #ifndef _OBJECT_H

    #define _OBJECT_H

    //位置

    class Object

    {

    public:

      NewType ToType();

    };

     

    #endif

     

     

    //在文件NewType.h 中定义

    #ifndef _NEWTYPE_H

    #define _NEWTYPE_H

     

    #include "Object.h"

     

    class NewType : public Object

    {

    }

     

    #endif

     

    产生错误:

    "error C2146: syntax error : missing ';' before identifier 'ToType'"

    "error C2501: 'NewType' : missing storage-class or type specifiers"

    原因是不能识别NewType类

     

     

    解决方案:

     

    于是在"位置"加上前向引用声明

    class NewType;

     

    编译通过

    但采用此种做法,类的定义和实现部分不能为内联函数,或者报错

    "error C2027: use of undefined type 'NewType'"

     

     

     

     

     

  • 相关阅读:
    Linux下Maven的安装与使用
    Vue1.0用法详解
    一个异步访问redis的内存问题
    jquery和zepto的异同
    我的学习归纳方法(以学习Maven为例)
    最显而易见的设计最容易成功
    Linux Command Backup
    Turn and Stun server · J
    Android apk签名详解——AS签名、获取签名信息、系统签名、命令行签名
    Leetcode 981. Time Based Key-Value Store(二分查找)
  • 原文地址:https://www.cnblogs.com/fengting/p/4583875.html
Copyright © 2020-2023  润新知