具有前向类声明的朋友类无法编译 [英] friend class with forward class declaration does not compile

查看:177
本文介绍了具有前向类声明的朋友类无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个基本程序,用于了解如何在C ++中使用friend class.

This a basic program to understand how to use friend class in C++.

xxx使用friend具有类yyy对象.从yyy类开始 是在类xxx之后定义的,我已使用forward声明了类yyy 声明.

Class xxx has a class yyy object using friend. Since class yyy is defined after class xxx I have declared class yyy using forward declaration.

#include<iostream>
using std::cout;
using std::endl;

class yyy; //Forward Declaration of class yyy

class xxx{
private:
  int a;

public:
  xxx(){a=20;yyy y2;y2.show();}       //Error//
  void show(){cout<<"a="<<a<<endl;}
  friend class yyy; //Making class yyy as freind of class xxx
};
class yyy{
private:
  int b;

public:
  yyy(){b=10;}
  void show(){cout<<"b="<<b<<endl;}
};

int main(int argc, char *argv[])
{
  xxx x1; //creating xxx object and calling constructor
  x1.show();
  return 0;
}

编译程序时出现此错误:

When I compile the program I get this error:

错误:汇总的"yyy y2"类型不完整,无法定义

error: aggregate ‘yyy y2’ has incomplete type and cannot be defined

我引用了此链接 递归朋友类 有人这样回答 https://stackoverflow.com/a/6158833/2168706

I referred to this link recursive friend classes and someone had answered like this https://stackoverflow.com/a/6158833/2168706

我正在采用这种方法,但仍然无法解决 问题.如果有的话,请提供解决方案,并让我知道 如果我在代码中的任何时候都错了.

I am following this approach but I am still unable to solve the issue. Please provide a solution if any exists and please let me know if I am wrong at any point in the code.

推荐答案

您要在此处实例化不完整类型yyy的对象y2:

Your're instantiating an object y2 of incomplete type yyy here:

{ a = 20; yyy y2; y2.show(); } 

将构造函数的实现移到yyy类的定义下方:

Move the implementation of constructor below the definition of yyy class:

class yyy;

class xxx {
  private:
    int a;

  public: 
    xxx();

    void show() { cout << "a=" << a << endl; }

    friend class yyy;
};

class yyy {
  private:
    int b;

  public:
    yyy() { b = 10; }

    void show() { cout << "b=" << b << endl; }
};

xxx::xxx() { a = 20; yyy y2; y2.show(); } // No error

因此,此时已经定义了yyy,您可以实例化y2.

As a result, at this point yyy is already defined, and you can instantiate y2.

为了给您一个逻辑上的解释,为什么您的变体不起作用:当您使用自动存储持续时间(在堆栈上)实例化yyy y2;之类的对象时,编译器必须知道编译时,它应该为y2保留多少内存.由于yyy类型不完整(仅在实例化时才进行声明),因此编译器会丢失并报告错误.

To give you a logical explanation, why your variant didn't work: when you instantiate an object with automatic storage duration (on stack) like yyy y2;, compiler has to know at compile-time how much memory it should reserve for y2. Since the yyy type is incomplete (was only forward declared at the point of instantiation), the compiler is lost and reports an error.

注意:最佳实践当然是通过将定义移到头文件(.hpp)和将实现移到源文件(.cpp)来分离类及其实现的定义.不要忘记适当地包含标题.我不想在这里给您一个示例,因为它是非常基本的内容,任何C ++书籍或教程都应涵盖.

NOTE: The best practice, is of course to separate definition of the class and its implementation by moving definition to header file (.hpp) and implementation to source file (.cpp). Don't forget to include headers properly. I don't feel like giving you an example here because it's very basic stuff and should be covered by any C++ book or a tutorial.

这篇关于具有前向类声明的朋友类无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆