如何确保#include 不会递归添加只添加一次唯一文件

问题描述

我有这个文件 main.h 和这个文件 events.h文件 events.h 需要包含 main.h 作为我的 Events 类的单例需要我的单例我的 Main 类,以便将 Main 声明为 friend 类,以便它可以访问 Events 私有成员/方法

相反,Main 也需要包含 Events调用它的方法,但是在 main 中执行 #include "events.h",然后在 events 中执行 #include "main.h" 只会递归地继续粘贴标题每个文件中的代码

我怎样才能避免这种情况,以便每个文件只在一次中粘贴代码

ma​​in.h

#ifndef MAIN_H
#define MAIN_H

#include "events.h"

class Main
{
public:
    Main(const Main&) = delete;
    Main(Main&&) = delete;
    Main& operator=(const Main&) = delete;
    Main& operator=(Main&&) = delete;


    static void Mainloop();




private:
    Main()
    {
        std::cout << "Main constructor called\n";
        mainloopInstanceBlocker = 'C';
    }

    static Main& Get_Instance()
    {
        static Main instance;
        return instance;
    }


    static void Init();
    static void Free();



    SDL_Window* window = nullptr;
    SDL_Renderer* renderer = nullptr;

    bool running = false;
    char mainloopInstanceBlocker = 'I';



};








#endif // MAIN_H

events.h

#ifndef EVENTS_H
#define EVENTS_H


#include <iostream>
#include <SDL2/SDL.h>
#include <string>


#include "main.h"


class Events
{
public:
    Events(const Events&) = delete;
    Events(Events&&) = delete;
    Events& operator=(const Events&) = delete;
    Events& operator=(Events&&) = delete;



    static const std::string& User_Text_input();




private:
    Events()
    {
        std::cout << "Events constructor called\n";
    }

    static Events& Get_Instance()
    {
        static Events instance;
        return instance;
    }


    friend class Main;

    ///For event handling
    static void Event_Loop()
    {
        Get_Instance();

        if (Get_Instance().eventLoopCounter == 0)
        {

            Get_Instance().eventLoopCounter += 1;

            while (SDL_PollEvent(&Get_Instance().event))
            {
                if (Get_Instance().event.type == SDL_QUIT)
                {
                    Get_Instance().m_quit = true;
                    break;
                }
                else if (Get_Instance().event.type == SDL_TEXTINPUT)
                {
                    Get_Instance().m_User_Text_Input = Get_Instance().event.text.text;
                }


            }

        }
    }

    ///For event handling
    static void Reset_Events()
    {
        Get_Instance().eventLoopCounter = 0;

        Get_Instance().m_quit = false;
        Get_Instance().m_User_Text_Input = "";
    }

    ///For quitting,used main only
    static bool Quit_Application(){
        return Get_Instance().m_quit;
    }



    ///For Event_Loop()
    int eventLoopCounter = 0;
    SDL_Event event;




    bool m_quit = false;

    std::string m_User_Text_Input = "";



};



#endif // EVENTS_H

解决方法

这是链接器和头文件的问题,你只需要在每个头文件的顶部添加#pragma once,编译器只会添加每个头文件的内容一次,但你必须确保你只需在您的头文件中声明方法并将这些方法的内容写入其他 .cpp 文件中,以防止链接错误。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...