2013-07-30 12 views
9

私はこのようなクラスがあります。Foom_valueがプライベートである(と私は維持したいと思いますのでフレンド機能

template<class X, class Y> 
class Blarg : public Bar { 
    // ... 
    void SetValue(double _val) { Foo::m_value = _val; } 
    // ... 
}; 

#include "Blarg.h" 
// ... 

class Foo : public Bar {  
    // ... 
    static double m_value; 
    // ...  
}; 

そして、このような別のものをそのように)、私はFooクラスの友人としてSetValue関数を宣言して、必要なときに静的メンバーにアクセスできると考えました。

template<class X, class Y> friend void Blarg<X, Y>::SetValue(double _val); 

template<class X, class Y> friend void Blarg::SetValue(double _val); 

friend void Blarg::SetValue(double _val); 

を...しかし、コンパイルするには運:

私はFooの公共エリア内のこれらの線に沿っての宣言を試していないしました。可能であれば、これに対する正しい構文は何ですか?

+0

最初のエラーではどのようなエラーが発生しますか? – Praetorian

+1

「コンパイル中の不運」は、直面したエラーの技術的な説明ではありません。 –

+0

各クラス定義の後に ';'を書く必要があります。 –

答えて

7

あなたはfriendとしてBlargの方法のいずれかをマークするためにFooクラスの前にBlargクラスを定義する必要があります。フレンドラインでFoo宣言の前にBlargが定義(または含まれています)されていますか?ここで

+2

これは、メンバ関数の型を修正します(戻り型がありません)。 +1 –

+0

^おっと!それをキャッチするためにありがとう! – nicole

0

は正しい構文です:

また
template<class T> 
class Bla 
{ 
public: 
    void toto(double val); 
};  

class Foo { 
    static double m_value; 
    template<typename T> 
    friend void Bla<T>::toto (double); 
} ; 

BlaFoo前に定義されていることを確認してください。

3

これは私のために働くようだ:

template<class X, class Y> 
class Blarg : public Bar { 
    public: 
     void SetValue(double _val); 
}; 

class Foo : public Bar { 
    private: 
     static double m_value; 

    public: 
     template<class X, class Y> friend void Blarg<X,Y>::SetValue(double _val); 
}; 

template <class X, class Y> 
void Blarg<X,Y>::SetValue(double _val) 
{ 
    Foo::m_value = _val; 
} 

私が最初Blargを定義し、値の代入ないインラインを行うことで循環依存関係を壊す必要がありました。あなたの友人宣言は、欠けている戻り値を除いて、かなり正確でした。

関連する問題