【问题标题】:Making a methods friend in nested namespaces在嵌套命名空间中与方法成为朋友
【发布时间】: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


【解决方案1】:

你可以这样做:

  1. MainComponent.h 中使用Include guards,并从中取出#include "Time.h"
  2. 将您的MainComponent.run() 函数放入.cpp 文件中,并在其中包含Time.hMainComponent.h
  3. Time.h 中包含MainComponent.h

所以你最终会得到这样的结果:
MainComponent.h:

#ifndef __MAIN_COMPONENT_H__
#define __MAIN_COMPONENT_H__
#pragma once
#include <chrono>
#include "..\graphics\Window.h"
#include "..\input\Input.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;

};
} }
#endif

Time.h

#include "MainComponent.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.cpp:

#include "MainComponent.h"
#include "Time.h"
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();
        }
    }
    }}

如果您想要其他解决方案,可以阅读here(使用前向声明,每个类都保留指向另一个类的实例的指针)。

【讨论】:

  • 这修复了它。有些我想得太远了。
猜你喜欢
  • 1970-01-01
  • 2018-10-20
  • 2019-11-18
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
  • 2010-12-29
相关资源
最近更新 更多