程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> vc教程 >> VC++ 在兩個文件互相包含時會出現的錯誤

VC++ 在兩個文件互相包含時會出現的錯誤

編輯: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 stora

[1] [2] 下一頁

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved