首页 > 解决方案 > 朋友声明调用“C++ 需要所有声明的类型说明符”

问题描述

当我只包含我的“Int.h”时,朋友声明会调用“C++ 需要所有声明的类型说明符”。当我只写前向声明“class Int;”时它运行良好 并在 cpp 文件中包含“Int.h”。

#ifndef _MY_ARRAY_H_
#define _MY_ARRAY_H_
// class Int;
#include "Int.h"

class MyArray {
    friend Int; //error: C++ requires a type specifier for all declarations
public:
    MyArray(int dim, int *size);
    Int operator[](int n);
private:
    int mDim;
    int *mSize;
    int *mData;
};
#endif

在我看来,它不需要“前向声明”,因为“Int.h”文件中有一个“Int”类声明。但确实如此。为什么?

标签: forward-declaration

解决方案


推荐阅读