我有两个类之间的朋友函数的问题。让我们来看看一些代码:朋友函数和包含循环
第一类:
#ifndef _FIRST_H_
#define _FIRST_H_
//#include "Second.h"
#include <string>
class Second;
class First
{
friend void Second::fun();
std::string str = "Dziala\n";
public:
First();
~First();
};
#endif
,二类:
#ifndef _SECOND_H_
#define _SECOND_H_
#include<iostream>
#include "First.h"
class Second
{
First fObj;
public:
Second();
~Second();
void fun() { std::cout << fObj.str; }
};
#endif
是没有问题的,如果我试图让友元类。如果我像上例中的朋友FUNCTION一样发生问题。 我可以通过#include“Second.h”在First class中解决这个问题,但它会包含循环。你有什么想法如何做到这一点?
'#include'循环没有问题,不是当你用'#ifndef _FIRST_H_' –
@MadsMarquart启动它们时,不是这样。这将是一个问题。 –
另外,我不明白你为什么想要这样做?检索字符串似乎很没用。 –