如何确保#include 不会递归添加(只添加一次唯一文件)
How to ensure #include doesn't recursively add (only adds unique files once)
我有这个文件 main.h
和这个文件 events.h
,文件 events.h
需要包含 main.h
作为我的 Events
class 需要我的 Main
class 的单身人士才能将 Main
声明为 friend
class,因此它可以访问 Events
private members/methods.
相反,Main
也需要包含Events
来调用它的方法,但是在main中做#include "events.h"
,然后在events中做#include "main.h"
只会递归地继续粘贴它们每个中的头文件代码。
我怎样才能避免这种情况,以便每个文件只将代码粘贴到 一次?
main.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
文件以防止链接错误。
我有这个文件 main.h
和这个文件 events.h
,文件 events.h
需要包含 main.h
作为我的 Events
class 需要我的 Main
class 的单身人士才能将 Main
声明为 friend
class,因此它可以访问 Events
private members/methods.
相反,Main
也需要包含Events
来调用它的方法,但是在main中做#include "events.h"
,然后在events中做#include "main.h"
只会递归地继续粘贴它们每个中的头文件代码。
我怎样才能避免这种情况,以便每个文件只将代码粘贴到 一次?
main.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
文件以防止链接错误。