头文件包括彼此

cha*_*255 0 c++

我有两个头文件.decimal.h和integer.h,每个都包含它们各自的类.我想写这样的东西.

//integer.h
#ifndef INTEGER_H
#define INTEGER_H
#include "decimal.h"
class Integer
{
     ...
     operator Decimal();
}
#endif

//decimal.h
#ifndef DECIMAL_H
#define DECIMAL_H
#include "integer.h"
class Decimal
{
     ...
     operator Integer();
}
#endif
Run Code Online (Sandbox Code Playgroud)

给我带来麻烦的是,因为它们包含了它,所以它在Visual Studio中表现得很奇怪并且产生奇怪的编译器错误.有没有办法解决?

Yon*_* Wu 6

也许你只想要一个前瞻宣言?

// In Integer.h
class Decimal;
class Integer
{
     ...
     operator Decimal();
};
Run Code Online (Sandbox Code Playgroud)

(顺便说一句,你错过了代码中的最后一个分号.)