【发布时间】:2016-12-27 17:34:25
【问题描述】:
以下问题:我目前正在将命名空间添加到我的项目中,但我似乎无法解决一件事,即嵌套命名空间中的交友方法。如果类 x 在命名空间 a 和命名空间 b 中,但是想要与类 x 中的方法成为朋友的类 y 在命名空间 a 和命名空间 b 中,则它不能工作,因为类 y 不知道命名空间 b,但是你不能包含类 x 的标题,因为类 x 已经包含类 y。有没有办法来解决这个问题 ?
我得到的错误:
'Kinnon::Time::Time::m_DeltaTime': 无法访问在类'Kinnon::Time::Time' 中声明的私有成员 'Main':不是类或命名空间名称 'Main': 不是类或命名空间名称
时间.h
#pragma once
namespace Kinnon { namespace Time {
class Time
{
friend void Main::MainComponent::run();
public:
static const float &getDeltaTime();
private:
Time();
static float m_DeltaTime;
};
} }
MainComponent.h
#pragma once
#include <chrono>
#include "..\graphics\Window.h"
#include "..\input\Input.h"
#include "..\time\Time.h"
namespace Kinnon { namespace Main {
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::duration<float> fsec;
class MainComponent
{
public:
MainComponent(const char *title, unsigned int width, unsigned int height);
~MainComponent();
void run();
void tick();
void render();
private :
Graphics::Window m_Window;
};
} }
MainComponent.run() 方法:
namespace Kinnon { namespace Main {
void MainComponent::run()
{
auto lastTime = Clock::now();
auto currentTime = Clock::now();
while(!m_Window.shouldClose())
{
lastTime = Clock::now();
tick();
render();
currentTime = Clock::now();
fsec passedTime = currentTime - lastTime;
Time::Time::m_DeltaTime = passedTime.count();
}
}
}}
【问题讨论】:
-
MainComponent.h似乎不需要包含Time.h。如果你把它去掉,那么Time.h可以包括MainComponent.h -
@igor 既然你这么说了。
-
@IgorTandetnik 如果这是答案,请将其作为答案。
标签: c++ namespaces nested