2016-08-20 13 views
0

次の問題:私は現在、プロジェクトにネームスペースを追加しています。ネストされたネームスペースでメソッドを呼び出せないようなことが1つあります。クラスxが名前空間aと名前空間bにあるが、友人にしたいクラスyであれば、クラスxのメソッドは名前空間aにあり、名前空間bはクラスyが名前空間bの手掛かりを持たないので動作できません。クラスxには既にクラスyが含まれているため、クラスxのヘッダを含めることはできません。これを修正する方法はありますか?私が手ネストされた名前空間でメソッドフレンドを作る

エラー:

'Kinnon ::タイム::タイム:: m_DeltaTime':プライベートメンバにアクセスすることはできません 'Kinnon ::タイム::時間' 'メイン' クラスで宣言:ではありませんクラスまたは名前空間名 'メイン':クラスまたは名前空間名は

ないTIME.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(); 
     } 
    } 
    }} 
+2

'MainComponent.h'は' Time.h'をインクルードする必要がありません。あなたがそれを取り除くと、 'Time.h'に' MainComponent.h'が含まれています。 –

+0

@igorあなたが言うようになりました。 –

+0

@IgorTandetnikそれが答えなら、それを答えてください。 –

答えて

0

次のようにあなたが行うことができます。

  1. 使用MainComponent.h内の警備員を含めて、それから#include "Time.h"を取ります。
  2. .cppファイルにMainComponent.run()関数を置き、Time.hMainComponent.hの両方を含めるようにしてください。
  3. は、MainComponent.hTime.hに含む。

ですから、このようなものになってしまいます:MainComponent.h
Time.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 

#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について読むことができます(前方宣言を使用し、各クラスは他のクラスのインスタンスへのポインタを保持します)。

+0

これはそれを修正しました。私はあまりにも遠くに考えていた。 –

関連する問題