gpt4 book ai didi

c++ - 包括两个文件之间的冲突 C++

转载 作者:行者123 更新时间:2023-11-28 01:32:07 25 4
gpt4 key购买 nike

我遇到了碰撞问题。我的意思是在我的 A.h 中需要包含 B.h 但在 B.h 中我需要包含 A.h 所以我不知道如何修复它。

接口(interface).h

#ifndef _INTERFACE_H
#define _INTERFACE_H

#include <SDL.h>
#include <vector>
#include "Widget.h"

class Interface
{
public:
Interface(SDL_Rect &r);
~Interface();
private:
SDL_Rect m_rect;
std::vector<Widget*> m_widgets;
};

#endif

小部件.h

#ifndef _WIDGET_H
#define _WIDGET_H

#include <SDL.h>
#include "Interface.h"

class Widget
{
public:
Widget(Interface *main, SDL_Rect &r);
~Widget();
private:
SDL_Rect m_rect;
Interface* m_master;
};

#endif

最佳答案

由于您依赖于指针,因此您可以声明(而不是定义)类,并在 cpp 文件中包含头文件:

#ifndef _INTERFACE_H
#define _INTERFACE_H

#include <SDL.h>
#include <vector>

class Widget; //See the swap from include to declaration?

class Interface
{
public:
Interface(SDL_Rect &r);
~Interface();
private:
SDL_Rect m_rect;
std::vector<Widget*> m_widgets;
};

#endif

在另一个标题中进行类似的交换。

关于c++ - 包括两个文件之间的冲突 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51060228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com